【发布时间】:2015-08-03 12:35:39
【问题描述】:
我将循环中的每 3 个元素包装在一个包装器 div 中,如下所示:
$query = array(
'post_type' => 'post',
);
$i = 1;
$posts = new WP_Query( $query );
$out = '<div class="wrapper">';
if ($posts->have_posts()){
while ($posts->have_posts()){
$posts->the_post();
$out.= '<div class="content">
//content here
</div>';
if($i % 3 == 0) {
$out .= '</div><div class="wrapper">';
}
$i++;
}
}
$out .= '</div>';
wp_reset_postdata();
return '<section>'.$out.'</section>';
这创建了一个很好的包装 html 减去一件让我烦恼的小事:
<section>
<div class="wrapper">
<div class="content"></div>
</div>
<div class="wrapper">
<div class="content"></div>
</div>
<div class="wrapper"></div>
</section>
如果我正好有 6 个帖子(或 3 的任何倍数,并且模数正在这样做),我会得到一个额外的空包装器。这真的不需要。
那么我应该在查询中包含什么条件以确保不会得到空包装器?
【问题讨论】: