codek
codek

Reputation: 343

Create div for every X divs

I have function right now:

<?php while ( have_rows('proyects') ) : the_row(); ?>

        <div class="box">
            <p><?php the_sub_field("texte");?></p>
        </div>

<?php endwhile; ?>

Which outputs something like this:

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

The problem is that I want it to output every three box a div container so the final result should look like this:

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

What would be the best way to do it?

Upvotes: 0

Views: 80

Answers (1)

Charles
Charles

Reputation: 437

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

Upvotes: 5

Related Questions