Categories
CMS Mastering Development Wordpress

How to pass a variable to get_template_part that’s updated every time the template part is called?

I have a loop and in that loop I use a get_template_part to call the HTMl for the post to display. I also have a variable (number) to which +1 is added every time the loop runs. In this way I can track which post is displayed (for a specific layout).

            <?php $loop_post_count++ ?>

            <!-- HTML code for loop -->
            <?php get_template_part ( 'loop') ?>

This all works fine, but now I want to have an if-condition in the loop.php file which depends on the value of $loop_post_count. To be more precise, it should determine if it is the first post in the query.

I also tried using the current_post value of the query:

if ($wp_query->current_post==0) : ?> 
    <               p>FIRST POST</p>
                        <?php endif;

But that didn’t work. It always outputs -1. Replacing $wp_query with $blog_posts (my variable containing the posts) outputs the same.

How do I pass a changing variable to the get_template_part or how do I check if it’s the first time get_template_part is called?

Many thanks in advance!

Leave a Reply

Your email address will not be published. Required fields are marked *