I am using a content page to render elements from my "Loop", I would like to count the post to get my current position as this would determine the size of the card I display. My (html, css cards you can see the bootstrap cards to understand what I mean) cards have variable width, with the first, second and third cards taking the majority of the space.
My code is below (for content.php):
<?php if ($count == 0) { ?>
<div class="row bg-white banner">
<div class="col col-sm-8 remove-left-margin">
<img src="./img/banner.png" class="banner-article-image" />
</div>
<div class="col-sm-4">
<div><p class="banner-article-topic"><?php the_category(); ?></p></div>
<div><h2 class="banner-article-title"><b><?php the_title(); ?></b></h2></div>
<div><p class="banner-article-par"><?php the_content(); ?></p></div>
<div><p class="banner-article-date"><?php the_date(); ?></p></div>
</div>
</div>
<?php } ?>
<?php if ($count > 2) { ?>
<div class="row">
<?php if ($count % 2 != 0) { ?>
<div class="col col-sm-6 custom-margin">
<div class="left-card-article">
<div class="row">
<img src="./img/sample-3.jpg" class="img-responsive card-article-image" />
</div>
<div class="row card-article-body">
<div><p class="left-article-topic"><?php the_category(); ?></p></div>
<div><h4 class="left-article-title"><b><?php the_title(); ?></b></h4></div>
<div><p class="left-article-date"><?php the_date(); ?></p></div>
</div>
</div>
</div>
<?php } ?>
<?php if ($count % 2 == 0) { ?>
<div class="col col-sm-6 custom-margin">
<div class="right-card-article">
<div class="row">
<img src="./img/sample-4.jpg" class="img-responsive card-article-image" />
</div>
<div class="row card-article-body">
<div><p class="right-article-topic"><?php the_category(); ?></p></div>
<div><h4 class="right-article-title"><b><?php the_title(); ?></b></h4></div>
<div><p class="right-article-date"><?php the_date(); ?></p></div>
</div>
</div>
</div>
<?php } ?>
</div>
<?php } ?>
This is a sample of my content.php, how can I pass the $count variable from index.php which is shown below (only the loop):
<div class="container body-margin" style="margin-top: 8em;">
<?php
$count = 0;
if ( have_posts() ) : while ( have_posts() ) : the_post();
get_template_part( 'content', get_post_format() );
$count++;
endwhile; endif;
?>
</div>
Would appreciate any help, thank you.
$count
variable you can useglobal $wp_query
object. It have$post_count
variable. So you directly write the code like$wp_query->post_count > 2
or$wp_query->post_count % 2 == 0
into the separate file.