I'd like to wrap every three posts in a new row (div) with a total of nine posts on a page. There's an empty row at the end. I thought this (I need to wrap every 4 wordpress posts in a div) would work, but I have more posts on pages 2, 3, 4, etc. Below is a simplified version of my code. $i = 1.
<div class="row">
<?php while ( have_posts() ) : the_post(); ?>
<div class="column">
</div>
<?php if ($i % 3 == 0 ) : ?>
</div> <!-- .row -->
<div class="row">
<?php endif; $i++; ?>
<?php endwhile; ?>
</div> <!-- .row -->
You can use get_next_post() to check whether next post exists or not.
<?php if ($i % 3 == 0 ) : ?>
</div> <!-- .row -->
<?php
$next_post = get_next_post();
if (!empty( $next_post )): ?>
<div class="row">
<?php endif; ?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With