将每三个 WordPress 帖子包装在 div 中

Jos*_*ern 5 php wordpress

我想将每三个帖子包装在一个新行 (div) 中,页面上总共有九个帖子。最后有一个空行。我认为这(我需要将每 4 个 wordpress 帖子包装在一个 div 中)会起作用,但我在第 2、3、4 页等页面上有更多帖子。下面是我的代码的简化版本。$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 -->
Run Code Online (Sandbox Code Playgroud)

Jos*_*ern 1

我用这篇文章(https://www.nosegraze.com/display-wordpress-posts-2-3-columns/)来解决我的问题。

$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; ?>
Run Code Online (Sandbox Code Playgroud)