为每个X div创建div

时间:2014-10-11 17:16:26

标签: php wordpress

我现在有功能:

<?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>

最好的方法是什么?

1 个答案:

答案 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>";
?>