Support

Account

Home Forums Add-ons Repeater Field Random Repeater in a Repeater

Solved

Random Repeater in a Repeater

  • I’m trying to get a repeater in a repeater to pull a random row. Here is what I have:

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

  • Hi @uteman23

    Your code is a bit messy.. 😉
    Also you’re not thinking correctly about the rand function. it will generate a random number. not select a random row in your repeater.

    Try this out:

    
    <?php 
    $i = 0; 
    while(have_rows('squares')): 
    	$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]['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; ?>
    
  • Hey Jonathan, thanks for your help.

    I tried your code and it sent the page into an infinite loop. Any other ideas?

  • Sorry I forgot to add the_row().

    
    <?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]['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; ?>
    
  • Jonathan, thank you so much. I had to add a few more parameters but we got it there. Thanks again.

    <?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; ?>
    
  • No problem! Glad to help out.

    Have a nice weekend

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

The topic ‘Random Repeater in a Repeater’ is closed to new replies.