我在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 –