Help understanding some code

  • Unknown's avatar

    Hi,

    A little long-winded but trying to explain clearly :)

    I am delving into the code of my new theme and am not familiar with this type of data storing/pulling – can someone help me understand what is going on here please:

    I am trying to create a page which outputs only the posts which have a ‘coupon’ attached to it. ‘Coupon’ is stored as a meta_key against a post.

    It’s meta_value is stored as an array which looks like this (output using print_r):

    a:7:{s:9:”highlight”;s:14:”Test Highlight”;s:5:”title”;s:12:”50% off food”;s:11:”description”;s:16:”Test description”;s:4:”code”;s:7:”1524521″;s:11:”popup_image”;s:4:”4548″;s:17:”popup_description”;s:17:”Popup description”;s:11:”redirect_to”;s:0:””;}

    I want the custom page to output:
    Post title
    Post content
    Post image
    Coupon details

    ————————————————–
    I can output the first three by running a wp_query:

        $q = new WP_Query(array(
            'post_type' => 'listing',
            'posts_per_page' => 5,
    	'meta_key' => 'coupon',
    	'meta_value' => ' ',
        	'meta_compare' => '!='
        ));

    However, I cannot find a way to split out the meta_value array to display the individual elements per post.

    ——————————————————————–

    The theme handles this in a way I don’t understand, perhaps someone can explain it to me.

    Coupon.php is a partial file which is used when displaying any single post which looks like this:

    global $post, $Args;
    use ListingToolsFrameworkHelpersGetSettings;
    
    $aCoupon = GetSettings::getPostMeta($post->ID, 'coupon');
    if ( empty($aCoupon) || ( empty($aCoupon['code']) && empty($aCoupon['redirect_to']) ) ){
    	return '';
    }
    ?>
    
        
    	    
        
    </div>

    —————————————————————-

    So I dont understand how the “use” code works at the top in the variables section.

    I also don’t understand how the $aCoupon = GetSettings::getPostMeta($post->ID, ‘coupon’); is working, but they can use this to display the individual parts of the coupon array as you can see later in the code:
    echo do_shortcode(‘[get_coupon highlight=”‘.esc_attr($aCoupon[‘highlight’])

    Hopefully that was follow-able :)

    Thanks in advance.

  • Unknown's avatar

    I can’t edit the above but a piece of code did not post correctly.

    Coupon.php looks like this:

    <?php
    global $post, $Args;
    use ListingToolsFrameworkHelpersGetSettings;
    
    $aCoupon = GetSettings::getPostMeta($post->ID, 'coupon');
    if ( empty($aCoupon) || ( empty($aCoupon['code']) && empty($aCoupon['redirect_to']) ) ){
    	return '';
    }
    ?>

    Followed by the divs which display the individual parts of the coupon array:
    <?php echo do_shortcode('[get_coupon highlight="'.esc_attr($aCoupon['highlight']).'" title="'.esc_attr($aCoupon['title']).'" description="'.esc_attr($aCoupon['description']).'" code="'.esc_attr($aCoupon['code']).'" redirect_to="'.esc_attr($aCoupon['redirect_to']).'"]'); ?>

  • Hi there,

    You have posted in the WordPress.com forums. This forum provides support to end-users of free sites hosted on WordPress.com.

    We’re not able to provide developer assistance here, nor do we provide support for the open source WordPress software found on WordPress.org, or any plugins created for that software.

    For help with your questions, you can try the advanced forum on WordPress.org, or perhaps the WordPress StackExchange:

    https://wordpress.org/support/forum/wp-advanced/

    https://wordpress.stackexchange.com/

    Or if you’re making these changes to a specific plugin, try the support forums for that specific plugin.

  • The topic ‘Help understanding some code’ is closed to new replies.