Wrap every three WordPress posts in a div

I would like to wrap every three posts in a new line (div), for a total of nine posts per page. There is an empty line at the end. I thought it was ( I need to wrap every 4 Wordpress posts in a div ), 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 -->
+4
source share
2 answers

I used this post ( https://www.nosegraze.com/display-wordpress-posts-2-3-columns/ ) to solve my problem.

$ i = 0;

<?php while ( have_posts() ) : the_post(); ?>

    <?php if ( $i == 0 ) : ?>

        <div class="row">

    <?php endif; ?>

    <div class="column">
    </div> <!-- .column -->

    <?php
        $i++;
        if( $i == 3 ) :
        $i = 0; ?>

        </div> <!-- .row -->

    <?php endif; ?>

<?php endwhile; ?>

<?php if ( $i > 0 ) : ?>

    </div> <!-- .row -->

<?php endif; ?>
0
source

get_next_post(), , .

<?php if ($i % 3 == 0 ) : ?>
</div> <!-- .row -->
<?php
    $next_post = get_next_post();
    if (!empty( $next_post )): ?>
        <div class="row">
    <?php endif; ?>
+2

Source: https://habr.com/ru/post/1657169/


All Articles