Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrap every three WordPress posts in div

Tags:

php

wordpress

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 -->
like image 968
Josh Kern Avatar asked Oct 19 '22 00:10

Josh Kern


1 Answers

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; ?>
like image 199
Amol Wankhede Avatar answered Oct 21 '22 03:10

Amol Wankhede