Support

Account

Home Forums Add-ons Repeater Field Random repeater

Solved

Random repeater

  • 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; ?>
  • 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?

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

  • 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
    	}
     
    	
    }
    
    ?>
    
  • 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 } } ?>
    
Viewing 11 posts - 1 through 11 (of 11 total)

The topic ‘Random repeater’ is closed to new replies.