Support

Account

Home Forums Add-ons Repeater Field Random repeater

Solved

Random repeater

    • jmeiii

    • September 23, 2013 at 10:34 am

    Hi, I have a repeater field, with 3 sub fields (image, link, title).

    I’ve randomised the image, however I can not seem to keep the link and the title with the randomed image. please help!

    		<?php if(get_field('career_grid')): ?>
    		
    			<?php while(the_repeater_field('career_grid')): ?>
    			
    				<?php
    		
    				$rows = get_field('career_grid' ); // get all the rows
    				$rand_row = $rows[ array_rand( $rows ) ]; // get the first row
    				$rand_row_image = $rand_row['image']; // get the sub field value 
    
    				$image = wp_get_attachment_image_src( $rand_row_image, 'full' );
    				?>	
    	    		
    				<a href="<?php the_sub_field('link'); ?>">
    					<img src="<?php echo $image[0]; ?>" alt="<?php echo $image['alt']; ?>" />
    				</a>
    	    	
    	    		
    	    	<?php endwhile; ?>
    	
    		<?php endif; ?>
    • Elliot

    • September 23, 2013 at 12:43 pm

    Hi @jmeiii

    You are using 2 completely different styles of code to load the image vs link. You need to use consistent code.

    Are you trying to randomise all the rows, then loop through them and output all rows in a random order?

    Or are you just trying to only output 1 random row?

    • jmeiii

    • September 23, 2013 at 2:17 pm

    yes i am trying to randomise all the rows, then loop through them and output all rows in a random order

    • Elliot

    • September 23, 2013 at 3:10 pm

    Hi @jmeiii

    Here is a snippet of code for you to use.

    It will load the repeater field, then shuffle the rows and then loop through the rows and render the HTML.

    Please note you can’t use any of the sub field functions with this code:

    
    <?php 
    
    $rows = get_field('career_grid');
    if($rows)
    {
    	shuffle( $rows )
     
    	foreach($rows as $row)
    	{
    		$image = wp_get_attachment_image_src( $row['image'], 'full' );
    		
    		?>
    		<a href="<?php echo $row['link']; ?>">
    			<img src="<?php echo $image[0]; ?>" alt="<?php echo $image['alt']; ?>" />
    		</a>
    		<?php
    	}
     
    	
    }
    
    ?>
    
    • jmeiii

    • September 23, 2013 at 3:20 pm

    thanks!

  • Hey! How do we limit it so that it only shows 10 rows… even if you have more than 30 rows entered?

  • This reply has been marked as private.
  • @bounty_digital Did you find out how to get a random AND limited output?

  • Count and Break.

    <?php 
    
    $rows = get_field('career_grid');
    if($rows){
    	shuffle( $rows )
            $i = 0;
     
    	foreach($rows as $row){
    		$image = wp_get_attachment_image_src( $row['image'], 'full');
    		
    		?>
    		<a href="<?php echo $row['link']; ?>">
    			<img src="<?php echo $image[0]; ?>" alt="<?php echo $image['alt']; ?>" />
    		</a>
    		<?php
                      if (++$i == 2) break;
             }
     
    	
    }
    
    ?>
  • I ended up with this:

    <?php $rows = get_field('field'); if($rows) $i = 0; { shuffle( $rows ); foreach($rows as $row) { $i++; if($i==13) break; $image = wp_get_attachment_image_src( $row['subfield1'], 'full' ); ?>
    <div class="class"><a href="<?php echo $row['subfield2']; ?>"><img src="<?php echo $image[0]; ?>" alt="<?php echo $image['alt']; ?>" /></a></div>
    <?php } } ?>
  • I was having a lot of trouble with this as well, especially with the attached images. Here is what I ended up with:

    <?php 
      $rows = get_field('partners'); 
      if($rows) $i = 0; { 
        shuffle( $rows ); 
        foreach($rows as $row) { 
          $i++; if($i==6) break; 
          $image = $row['partner_logo']; ?>
    
          <li class="partners-list-item desaturate"><img src="<?php echo $image; ?>" alt="" /></li>
    <?php } } ?>
    
  • Hello,

    Working with Images – URL

    ” alt=”” />

  • If you only want one random item from ACF repeater, you can also do:

    $repeater = get_field( ‘repeater_field_name’ );
    $rand = rand(0, (count($repeater) – 1));
    echo $repeater[$rand][‘sub_field_1’];

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.