Home › Forums › Add-ons › Repeater Field › 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?
@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 } } ?>
The topic ‘Random repeater’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.