Jump to content

Random image from repeater based gallery


jothanne
 Share

Recommended Posts

I have a problem realizing a picture gallery with albums using repeater fields.
Here is what I have:
- The repeater field 'repeater_gallery' for all the albums
- A text field 'album_name' in the repeater for the album name
- An image field 'album_images' in the repeater for all the album images

Now I'd like to pick a small selection of images (say 4) of ALL albums randomly and present them with their associated album name.

How can this be done with the described repeater setup? Or is there a better way to realize such a gallery without repeaters?

Thanks in advance for a little help.

Link to comment
Share on other sites

@AndZyk - Thanks for your reply,

the 'getRandom()' method is well known, but I think with your solution I only get 4 randomly choosen ALBUMS and not the desired 4 IMAGES out of ALL ALBUMS.
But maybe I didn't grasped it right ...

@szabesz - Thank you for the link,

I've already seen this post but was a bit distracted by the JS an slideshow discussion in there. Maybe I can extract some parts to solve my problem.

As far as I can see, a possible solution is to randomly choose a subset of 4 albums and in the next step select 1 ( only one) image out of all the images in the image field of each choosen album - hmmm ...

Any further tips?

Regards, Jochen

  • Like 1
Link to comment
Share on other sites

Ah okey, I should have read more carefully.

Do you want for example four random images, even if they are from the same album?

You could try to achieve this by an selector:

Quote

Finding pages by repeater value (using selectors)

Use selector subfields to locate pages by a value in a repeater field. Subfields are those that are split with a period, like "field.subfield". This is best demonstrated by examples, again continuing with our buildings field, we will locate pages that have a buildings repeater field by properties of the individual buildings within that repeater field: 


// find all pages that have at least one building with a height > 500 feet
$buildings = $pages->find("buildings.height>500");   

Or if you don't want the case of four random images from the same album to happen, you could use further:

<?php
foreach ($page->repeater_gallery->getRandom(4) as $item) {
  	$randomImage = $item->album_images->getRandom();
	echo "<img src='{$randomImage->url}'>"; 
}

 

Link to comment
Share on other sites

@AndZyk - Thanks a lot,

meanwhile I got it up and running. No surprise that it's more or less nearly the same as your last posted solution.

<?php
$albums = $page->repeater_gallery->getRandom(4);
if (count($albums)) {
	foreach($albums as $album) {
		$image = $album->images_album->getRandom();
		if ($image) {
			$imageUrl = $image->url;
			
			// Output

		}
	}
}
?>

The only flaw with this (for my special need) is, that if there are less albums as specified in the 'getRandom()' method, you do not get 4 images displayed. But since this will rarely occur I can live with it in the first place.

Regards, Jochen

  • Like 1
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

×
×
  • Create New...