Roych Posted August 31, 2017 Share Posted August 31, 2017 Hello, need your help 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 More sharing options...
DaveP Posted August 31, 2017 Share Posted August 31, 2017 Any reason for using a repeater? (Image fields can hold multiple images anyway, and much easier to manipulate.) See 1 Link to comment Share on other sites More sharing options...
psy Posted August 31, 2017 Share Posted August 31, 2017 @Roych On 8/31/2017 at 11:11 AM, Roych said: <?php foreach($pages->get('/settings/')->BackgroundImages->getRandom() as $bck): ?> Expand 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): ?> 9 Link to comment Share on other sites More sharing options...
Roych Posted September 1, 2017 Author Share Posted September 1, 2017 On 8/31/2017 at 9:10 PM, psy said: <?php foreach($pages->get('/settings/')->BackgroundImages->shuffle() as $bck): ?> Expand Sorry forl late response. Exactly what I needed, working great Thank you very much Didn't know about "shuffle" R 2 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now