Wordpress循环:将3个搜索结果换行


Wordpress Loop: Wrap 3 search results in a row

我已经设置了我的模板来输出col-4中的搜索条目,以形成一个3列布局,并且我需要将每组3列包装成一行。

我已经提出了以下内容,但我的打开和关闭PHP似乎是错误的,因为我目前在自己的行中获得了每个1个搜索条目。提前感谢的帮助

<?php 
if ($counter % 3 == 0) { 
  echo '<div class="row">';
} ?>

<article id="post-<?php the_ID(); ?>" class="search-result col-md-4">
    <?php
    $entry_thumbnail = racks_entry_thumbnail();
    $entry_class = $entry_thumbnail ? 'search-thumbnail' : 'col-sm-4 col-md-4';
    ?>
        <?php if( $entry_thumbnail ) : ?>
            <div class=" <?php echo esc_attr( $entry_class ); ?>">
                <?php echo $entry_thumbnail; ?>
            <header class="entry-header clearfix">
                <h2 class="entry-title"><a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>" rel="bookmark"><?php the_title() ?></a></h2>
                </header>
        <div class="entry-summary">
          <?php the_excerpt(); ?>
        </div>
        <footer class="entry-footer">
          <a class="read-more" href="<?php the_permalink() ?>"><?php _e( 'Read more', 'racks' ) ?></a>
        </footer>
    </div>
        <?php endif;?>
    <div class="clearfix"></div>
</article><!-- #post-## -->
<?php $counter++ ; echo '</div>'; ?>

别忘了,对于可被3:整除的$counter,您只需要关闭</div>

<?php 
  echo ($counter % 3 === 0) ? '</div>' : '';
  $counter++;
?>

现在,在循环的每一步都关闭</div>。还要注意,您可能希望在迭代开始时进行此检查,将其结果分配到变量中。例如:

<?php
$counter = 0;
while ($counter < someLimit):
  $newRow = $counter % 3 === 0;
?>
  // at the beginning of template:
  <?= $newRow ? '<div class="row">' : ''; ?>
  // output goes here
  // at the end of template:
  <?= $newRow ? '</div>' : ''; ?>
<?php endwhile; ?>

另一种方法是使用输出缓冲区来存储某个数组中每个元素的输出,将该数组拆分为块(使用array_cchunk()),然后输出这些块(将每个块封装在<div class="row"></div>结构中)。