0
votes

I'm using Advanced Custom Fields and have a repeater in a repeater. I need the nested repeater to pull one row randomly. Here is what I have that isn't working:

<?php $i = 0; while(the_repeater_field('squares')): ++$i;
        $repeater = get_sub_field( 'images' );
        $rand = rand(0, (count($repeater) - 1));?>
        <a href="<?php the_sub_field('link'); ?>" class="fs-square" style="background-image:url('<?php echo $rand['four-square']; ?>')">    
            <div class="main-text"><?php the_sub_field('main_text'); ?></div>
            <div class="icon-section">
                <div class="icon"><?php the_sub_field('icon'); ?></div>
                <div class="icon-text"><?php the_sub_field('icon_text'); ?></div>
            </div>
        </a>
    <?php endwhile; ?>

Basically, I have a repeater for squares and then in that I have a repeater for images. I need the images repeater to be random.

2

2 Answers

0
votes

You may wanna try

<?php echo $repeater[$rand]['four-square']; ?>

instead of

<?php echo $rand['four-square']; ?>

HTH!

0
votes

This is what fixed it for me.

<?php 
    $i = 0; 
    while(have_rows('squares')): 
    the_row();
        $i++;
        $repeater = get_sub_field( 'images' );
        $rand = rand(0, (count($repeater) - 1)); //does not select a specific row but rather just a number
        ?>
        <a href="<?php the_sub_field('link'); ?>" class="fs-square" style="background-image:url('<?php echo $repeater[$rand]['image']['sizes']['four-square']; ?>')">   
            <div class="main-text"><?php the_sub_field('main_text'); ?></div>
            <div class="icon-section">
                <div class="icon"><?php the_sub_field('icon'); ?></div>
                <div class="icon-text"><?php the_sub_field('icon_text'); ?></div>
            </div>
        </a>
    <?php endwhile; ?>