First and Shuffle

Hello!

What is the best way to pull images from the first child of page and have them shuffled?

I’m able to pull the first image without any problems, but the shuffle() function does not seem to work?

Is there any work around this?

<?php echo $page->children()->first()->shuffle()->url() ?>

Xx

You were missing getting the image collection:

<?php echo $page->children()->first()->images()->shuffle()->first()->url() ?>
1 Like

Note that if the image doesn’t exist, calling the URL method will throw an error. It’s therefore better to do it like this:

<?php 
if($image =  $page->children()->first()->images()->shuffle()->first()) {
  echo $image->url();
}
?>
3 Likes