Support

Account

Home Forums Add-ons Repeater Field Is there a way to separate repeater rows by groups of five?

Solving

Is there a way to separate repeater rows by groups of five?

  • Like let’s say I have a design where I will need the first five instances of a repeater. Then a visible code changes happens, then you show the next five instances. repeat until the rows are done.

  • <?php
    $count = 0;
    $your_repeater = get_field('your_repeater');
    if($your_repeater){
       echo '<div class="headerrow">header</div>';
       while( have_rows('your_repeater') ): the_row();
       $count++;
       $my_field = get_sub_field('my_field');
       echo '<div class="row">'.$my_field.'</div>';
       if ($count % 5 == 0) {
          echo '<div class="specialrow">subheader</div>';
       }
    endwhile;  
    }
    ?>

    with that code every 5 row a additional row is added (always the same)
    if you need each 5 row totally different code, replace
    if ($count % 5 == 0) with if ($count == 5) and add a if-loop for each additional 5 rows

  • Not quite what I meant.

    What I need to be able to do is get the first five rows of a repeater, repeat the first five rows of that repeater in a different format, then do the next five rows and repeat the next five rows in a differetn format.

  • even than it is nearly the same. just more if loops. (or i misunderstand you completely)

    if you need something around, use loop before my_field, and one after like my example.

    without example what you like to do it is hard to see what i understand wrong.
    could you provide a sample what code you use and what you expect for frontend.

  • Ok, this is what I mean.

          <div class="tabz">
          
    <?php if( have_rows('employees') ): ?>
          <ul class="employees">
    <?php $count = 1; ?> 
    <?php while ( have_rows('employees') ) : the_row(); ?>
              
              <li><a href="#employee-<?php echo $count; ?>" style="background-image: url(<?php the_sub_field('photo'); ?>);"><div class="hover"><?php the_sub_field('title'); ?></div></a><strong><?php the_sub_field('name'); ?></strong></li> 
    
    <?php $count++; endwhile; ?>
            </ul>
    <?php endif; ?>
              
              
    <?php if( have_rows('employees') ): ?>
    <?php $count = 1; ?> 
    <?php while ( have_rows('employees') ) : the_row(); ?>            
          <div id="employee-<?php echo $count; ?>" class="employee-bio">
            <div class="row">
              <figure style="background-image: url(<?php the_sub_field('photo'); ?>);"></figure>  
              <article>
                <h2><?php the_sub_field('name'); ?></h2>
                <h3><?php the_sub_field('title'); ?></h3>
                
                <?php the_sub_field('biography'); ?>
    
              </article>
            </div>
          </div>  
    <?php $count++; endwhile; ?>
    <?php endif; ?>
              
          </div>

    I want to run both for every 5 rows

  • just to get sure. you wish:

    <div class="tabz">
    //ul with 5 rows employees
    //div with 5 rows employees
    </div>
    <div class="tabz">
    //ul with 5 rows employees
    //div with 5 rows employees
    </div>
    ...

    and both (ul and div) use same repeater

  • it is possible but you need to rebuild/change your code.
    you need to fill first all rows to variables or a array.
    and use that to echo values. (here i use variables)

    simple it would look like that:

    <?php
    $count = 0;
    $list_employee = '';
    $div_employee = '';
    while ( have_rows('employees') ) : the_row();
    $count++;
    $list_employee .= '<li><a href="#employee-'; //whole code for li (no echo!, nothing outside php, get_field instead of the_field)
    $div_employee .= '<div id="employee-'; //whole code for div (no echo!, nothing outside php, get_field instead of the_field)
     if ($count % 5 == 0) { //each 5 rows echo ul and div
    echo '<div class="tabz"><ul class="employees">';
    echo $list_employee;
    echo '</ul>';
    echo $div_employee;
    echo '</div>';
          $list_employee = '';
          $div_employee = '';
       }
    endwhile;
    if ($list_employee){ //if all rows are done but 1-4 rows left, echo them
    echo '<div class="tabz"><ul class="employees">';
    echo $list_employee;
    echo '</ul>';
    echo $div_employee;
    echo '</div>';}
    ?>

    try to modify your code like above, just the whole one. this should work.
    if not: show whole modified code and what the “error/problem” is.

Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘Is there a way to separate repeater rows by groups of five?’ is closed to new replies.