Support

Account

Home Forums Add-ons Repeater Field Assign classes to repeater divs in iterations of 3

Solved

Assign classes to repeater divs in iterations of 3

  • Hi I am using a repeater to show slides in a slider and I would like to assign classes on repeat. So if I have 6 slides, their classes would look like so: num1, num2, num3, num1, num2, num3…etc. So only those 3 classes repeating over and over, no matter how many slides I have.

    I have the following code:

    <?php
    if( have_rows('slide') ): $i = 0; ?>
    
    <?php
    while ( have_rows('slide') ) : the_row(); $i++; ?>
    
    <div class="slide num-<?php echo $i; ?>">
    <p><?php the_sub_field('text1'); ?></p>
    <p><?php the_sub_field('text2'); ?></p>
    </div>
    
    <?php
    endwhile;
    endif; 
    ?>

    This code creates classes for the slides indefinitely, but not 3 repeating classes. Any help appreciated!

  • Here is the solution in case anyone is wondering:

    <?php
    
    if( have_rows('slide') ):
        $i = 1;
        while ( have_rows('slide') ) :
            the_row();
            // Check if we've reached our limit, and if so... start again! 
            if ( $i > 3 )
            {
                $i = 1;
            }
        ?>
    
            <div class="slide num-<?php echo $i; ?>">
                <p><?php the_sub_field('text1'); ?></p>
                <p><?php the_sub_field('text2'); ?></p>
            </div>
    
        <?php
            $i++;
        endwhile;
    endif;
    
    ?>
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Assign classes to repeater divs in iterations of 3’ is closed to new replies.