我在wordpress中使用循环来输出帖子。我想要将每三个职位包装在一个div中。我想在循环的每次迭代中使用计数器递增,但我不确定“如果$ i是3的倍数”或“如果$ i是3 - 1的倍数”的语法。PHP循环:围绕每三个条目添加div
$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个项目时,不做任何更改? – 2013-09-08 15:14:05
这是我可以找到的最简单的方法:http://stackoverflow.com/questions/28247770/loop-through-wordpress-posts-and-wrap-each-x-post-in-a-div –