Frage

I am struggling to find a solution for this problem:

<?php 
$counter = 1;
while (has_sub_field('company_members')) :  
    echo '<div class="row">';  
    while ($counter <= 3)  :
        get_template_part('team-member-box'); ++$counter;
    endwhile;
    echo '</div>'; 
    $counter = 1;
endwhile; 
?>

What I want the code to do is to print 3 "team-member-box" and then create a new row.

So far all the boxes have been collocated in one single row.

The code right now prints the same box for three times, instead I want it to take 3 elements in the repeater field "company_members" and print within a new row the next 3 elements.

Thank you in advance for your help.

War es hilfreich?

Lösung

Not sure if this is what you try to do but here is a general way to display list of data in 3 per row:

$counter = 0;

echo '<div class="row">';

while (has_sub_field('company_members')) {

    echo ($counter++ % 3 == 0) ? '</div><div class="row">' : '';

    get_template_part('team-member-box'); 

}

echo '</div>';

Explained:
has_sub_field it loops all company_members, I guess get_template_part renders some html using the current item. See the_sub_field('content') in the above link.

The $counter is not related to above behavior it is just used to separate the results in chunks of 3 and wrapping them in divs.

Andere Tipps

Thanks for this. And to fix the issue of creating an empty row at the beginning add this:

if($counter) :
  echo ($counter++ % 2 == 0) ? '</div><div class="row">' : '';
else :
  $counter++;
endif;
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top