Jump to content

Random repeater with image first()


Roych
 Share

Recommended Posts

Hello,

need your help :undecided:  I have repeater with only one image field and I would like it to be fetched randomly. It is used for a background images on a site.

my code:

<?php foreach($pages->get('/settings/')->BackgroundImages->getRandom() as $bck): ?>
	<?php $bckgimage = $bck->bgimage->first(); ?>
		<li><img data-fade='2000' src="<?php echo $bckgimage->url; ?>" alt="" /></li>
<?php endforeach; ?>

But it's not working and couldn't find the right answer on a forum ..

not sure how to do this

Thank you

R

Link to comment
Share on other sites

@Roych

 

9 hours ago, Roych said:

<?php foreach($pages->get('/settings/')->BackgroundImages->getRandom() as $bck): ?>

You are only retrieving one BackgroundImages item with getRandom(). To get multiple items for your Wire Array you need to specify how many you want. https://processwire.com/api/ref/wire-array/get-random/

Alternatively to retrieve all BackgroundImages in random order you could try:

<?php foreach($pages->get('/settings/')->BackgroundImages->shuffle() as $bck): ?>

 

  • Like 9
Link to comment
Share on other sites

13 hours ago, psy said:

 


<?php foreach($pages->get('/settings/')->BackgroundImages->shuffle() as $bck): ?>

 

Sorry forl late response.
Exactly what I needed, working great ;) Thank you very much ;) Didn't know about "shuffle" ;)

R

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...