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) : ? 
                   pFIRST 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!

Topic query-variable get-template-part loop variables php Wordpress

Category Web


I found an easy solution. Replacing

<?php get_template_part ( 'loop') ?>

with

<?php include( locate_template( 'loop.php', false, false ) ); ?>

did the trick. I could just use the variable as I usually would. Code and answer from mekshq.com.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.