xpnimi
1
I want to fetch the first 5 images from an invisble content/projects folder to use them in a slider in the “home” template
The content folder looks like this
content
01-home
02-about
projects
01-project-a
01-image-a.jpg
02-project-b
01-image-b.jpg
the following code does not work:
$sliderImages = $pages->get('projects')->children()->visible()->images();
You have to iterate through the children and then fetch the image for each child:
<?php foreach(page('projects')->children()->visible() as $child) {
$image = $child->images()->first();
}
?>
if you need the images in a new Collection, you can append each image to the collection within the foreach loop.
xpnimi
3
That helped a lot. Thank you!
I ended up with this:
$sliderImages = new ArrayObject();
$n = 0;
foreach(page('projects')->children()->visible()->flip() as $child) {
if ($n < 5) {
$sliderImages->append($child->image('01-head.jpg'));
$n++;
} else {
break;
}
}