我现在有功能:
<?php while ( have_rows('proyects') ) : the_row(); ?>
<div class="box">
<p><?php the_sub_field("texte");?></p>
</div>
<?php endwhile; ?>
其中输出的内容如下:
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
问题是我希望它每三个box
输出一个div容器,所以最终结果应如下所示:
<div class="container">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
<div class="container">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
<div class="container">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
最好的方法是什么?
答案 0 :(得分:5)
<?php
$counter = 0;
while ( have_rows('proyects') ) : the_row();
if($counter%3 == 0 && $counter != 0)
echo '</div>';
if($counter%3 == 0)
echo '<div class="container">';
$counter++;
?>
<div class="box">
<p><?php the_sub_field("texte");?></p>
</div>
<?php endwhile;
if ($counter != 0)
echo "</div>";
?>