The simplest solution (as in requiring the least work) would be to just make $count
a global variable.
So in index.php
you’d have:
global $post;
$count = 0;
And in content.php
you’d just need to use $global $post;
to access it:
<?php
global $post;
if ($count == 0) { ?>
However, I think this is the wrong approach. Your situation is that you effectively have 3 different templates and you want to choose which to display based on the current post count. The current post count is irrelevant to the individual templates. It’s something that should be determined in the loop.
So what I’d suggest is creating 3 different template files for each type of card. Then inside index.php choose which to display based on the current number. This way you don’t need to pass variables between templates.
So index.php would look like this:
$count = 0;
if ( have_posts() ) : while ( have_posts() ) : the_post();
if ( $count == 0 ) {
get_template_part( 'content', 'banner' );
}
if ( $count > 2 ) {
echo '<div class="row">';
if ( $count % 2 != 0 ) {
get_template_part( 'content', 'left-card' );
} else if ( $count % 2 == 0 ) {
get_template_part( 'content', 'right-card' );
}
echo '</div>';
}
$count++;
endwhile; endif;
Then you would split content.php
into content-banner.php
, content-left-card.php
, and content-right-card.php
with the template for each type of card.
PS: I just copied your if statements for $count
, but it looks to me like this loop would not output anything at all for the 2nd & 3rd posts. I’m not sure if that’s intentional, but if it isn’t, $count > 2
should probably be $count > 0
.