Sam Skirrow
Sam Skirrow

Reputation: 3697

Wordpress php can't show custom meta info on post listings page for each post

I have created a custom post type within WordPress, and for that custom post type I have custom meta boxes where I can input the price of a product rental either per day or per week (there are 2 meta boxes).

I want to display these prices on the page that displays all the posts from my custom post type. My theme has an action hook just after the title of the post which is where I want to display the meta info.

I have managed to hook into the action, however, my information only shows on the first post, not on all of them.

Here is the code I am using to echo out the meta info:

function add_prices_to_products() {
    global $post;
    $price_per_day = get_post_meta($post->ID, "_per_day", true); 
    $price_per_week = get_post_meta($post->ID, "_per_week", true); 
    echo '<span class="sl_product_price per_day">£' . $price_per_day . '/day</span>';   
    echo '<span class="sl_product_price per_week">£' . $price_per_week . '/wk</span>';   
}
add_action('layers_after_list_post_title', 'add_prices_to_products');

Can anyone tell me why it is just adding to the first post and not to all of them (I can confirm that the meta info is saving correctly)?

Here is the code of the page from the theme (index.php):

<?php get_header(); ?>

<div class="container content-main archive clearfix">
<?php get_sidebar( 'left' ); ?>

<?php if( have_posts() ) : ?>
    <div <?php layers_center_column_class(); ?>>
        <?php while( have_posts() ) : the_post(); ?>
            <?php get_template_part( 'partials/content' , 'list' ); ?>
        <?php endwhile; // while has_post(); ?>
        <?php the_posts_pagination(); ?>
    </div>
<?php endif; // if has_post() ?>

<?php get_sidebar( 'right' ); ?>
</div>
<?php get_footer();

it references the file content-list.php which is what contains the hook.

global $post, $layers_post_meta_to_display; ?>
<article id="post-<?php the_ID(); ?>" <?php post_class( 'push-bottom-large' ); ?>>
<?php do_action('layers_before_list_post_title'); ?>
<header class="section-title large">
    <?php do_action('layers_before_list_title'); ?>
    <h1 class="heading"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
    <?php do_action('layers_after_list_title'); ?>
</header>
<?php do_action('layers_after_list_post_title'); ?>

<?php /**
* Display the Featured Thumbnail
*/
echo layers_post_featured_media( array( 'postid' => get_the_ID(), 'wrap_class' => 'thumbnail push-bottom', 'size' => 'large' ) ); ?>

<?php if( '' != get_the_excerpt() || '' != get_the_content() ) { ?>
    <?php do_action('layers_before_list_post_content'); ?>
    <?php do_action('layers_list_post_content'); ?>
    <?php do_action('layers_after_list_post_content'); ?>
<?php } ?>

<?php do_action('layers_before_list_post_meta'); ?>
    <?php /**
    * Display the Post Meta
    */
    layers_post_meta( get_the_ID(), NULL, 'footer', 'meta-info push-bottom' ); ?>
<?php do_action('layers_after_list_post_meta'); ?>

<?php do_action('layers_before_list_read_more'); ?>
<?php do_action('layers_list_read_more'); ?>
<?php do_action('layers_after_list_read_more'); ?>
</article>

It's also worth mentioning that I am hooking in from a plugin

UPDATE

Using the hook "the_post" I am able to echo out plain text on each post in the list, however, I can't get my meta box info to echo yet. Here is what I'm using now:

add_action( 'the_post', 'my_custom_loop_start' );

function my_custom_loop_start( $query )
{
echo 'hello';
    add_action( 'loop_end', 'my_custom_loop_end' );
}

function my_custom_loop_end()
{
remove_action( 'layers_after_list_post_title', 'add_prices_to_products' );   
}

Upvotes: 0

Views: 134

Answers (1)

Simon Pollard
Simon Pollard

Reputation: 2588

You need to hook into each item in the loop, try something like this...

// Hook into the main loop    
add_action( 'loop_start', 'my_custom_loop_start' );

    // Add two filters in that loop - your existing function and one to clear it out
    function my_custom_loop_start( $query )
    {
        if( $query->is_main_query() )
        {
            add_filter( 'layers_after_list_post_title', 'add_prices_to_products' );
            add_action( 'loop_end', 'my_custom_loop_end' );
        }
    }

    // This will just remove your action once each loop is done so you get the correct data for each loop
    function my_custom_loop_end()
    {
        remove_action( 'layers_after_list_post_title', 'add_prices_to_products' );   
    }

You may need to tweak this to work properly, but the logic is adding your function to each loop, running it, then removing it.

Upvotes: 0

Related Questions