【发布时间】:2012-02-15 09:36:26
【问题描述】:
我在 wordpress 中使用循环来输出帖子。我想将每三个帖子包装在一个 div 中。我想在循环的每次迭代中使用计数器递增,但我不确定“如果 $i 是 3 的倍数”或“如果 $i 是 3 - 1 的倍数”的语法。
$i = 1;
if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post();
// If is the first post, third post etc.
if("$i is a multiple of 3-1") {echo '<div>';}
// post stuff...
// if is the 3rd post, 6th post etc
if("$i is a multiple of 3") {echo '</div>';}
$i++; endwhile; endif;
我该如何做到这一点?谢谢!
【问题讨论】:
-
如果我只想添加超过 3 个项目会怎样?当它等于 3 项时保持不变?
-
这是我能找到的最简单的方法:stackoverflow.com/questions/28247770/…
标签: php wordpress loops while-loop