How to output images from first specific pages on home

Hi,

I want to output images from the last pages in my home page.

Capture%20d%E2%80%99%C3%A9cran%20de%202019-10-23%2011-09-31
this is my architecture, I want to output the images from the first children of 1-works on the homepage.

I’ve tried first to output the url whith this code an it’s worked very well :

<?php echo $pages->find('works')->children()->first()->url() ?>

Now I’m trying to output the images with these lines :

<?php foreach($pages->find('works')->children()->first() as $preview): ?>
    <?php if($image = $preview->image($preview->slideshow()->yaml())) { echo $image->html(); }?>
 <?php endforeach ?>

I’m getting nothing…
I’ve got this error :

That line doesn’t make sense. You cannot loop through a single page.

<?php if (($page = $pages->find('works')) && $page->hasListedChildren()) : ?>
<?php $images = $page->children()->first()->images() ?>
    <?php if ($images->count() > 0) : ?>
        <?php foreach ($images as $image)  : ?>
        <!-- do something with image -->
        <?php endforeach ?>
    <?php endif ?>
<?php endif ?>

Although, looks like you are trying to get images from a structure field?

I’m trying to get images from a field made with this plugin : Slideshow plugin

Then this should work:

<?php if (($parent = $pages->find('works')) && $parent->hasVisibleChildren()) : ?>
    <?php $p = $parent->children()->first(); ?>
    <div class="slider">
    <?php foreach ($p->slideshow()->yaml() as $image) : ?>   
        <?php if ($image = $p->image($image)) : ?>
            <?= $image->crop(1200,500)->html(); ?>  		    
        <?php endif ?>
    <?php endforeach; ?>
    </div>

<?php endif ?>

It’s worked, but there is a little mistake on your lines. In the line 5 it’s not

<?php if ($image = $page->image($image)) : ?>

but

<?php if ($image = $p->image($image)) : ?>

Thank’s, I understood my mistake by making a loop