Thumbs in foreach-loop show only one image

Hi there,
I’m using the thumb function in a foreach loop, but only the first image is displayed and repeated as often as the loop has items.
Here’s my code:
<?php foreach ($galleryfiles as $galleryfile): ?>
<?php echo thumb($page->image(), array('width'=>300, 'crop'=>true)); ?>
<?php endforeach ?>
I’m sure, the solution is pretty simple, but I can’t see.

Thank you for help
Johannes

Well, you are calling $page->image() in each loop, so without any filename passed, this will always return the first image :wink:. Try this:

<?php foreach ($galleryfiles as $galleryfile): ?>
<?php echo thumb($page->image($galleryfile), array('width'=>300, 'crop'=>true)); ?>
<?php endforeach ?>
```

You should, however,  use an if statement to make sure that the image exists:

<?php foreach ($galleryfiles as $galleryfile): ?> <?php if($image = $page->image($galleryfile)): ?>
<?php echo thumb($image, array('width'=>300, 'crop'=>true)); ?>
<?php endif ?> <?php endforeach ?>

Thank you for your quick reply, Texnixe.
Unfortunately, neither with nor without the if statement I get the thumbs displayed. Not the first repeated as before, but no thumb at all.
Did I mistype anything? I even tried your code with copy-paste. No thumb at all.

How is $galleryfiles defined? Does $galleryfile give you a filename?

galleryfiles contains this fields in the yaml file:
text / type: text, for the alt attribute
picture / type image, e. g. gallery_01.jpg
notes /type text
filename / type text, for the filename without ending, e. g. gallery_01

Could you pls. post the complete code? Is that a structure field you are using?

Hasn’t there been another reply for some seconds?
Anyway, her’s the code from blueprints:

galleryfiles:
    label: Galerie
    type: structure

    fields:
      text:
        label: Text
        type: text
      picture:
        label: Bild
        type: image
      notes:
        label: Bemerkungen
        type: text
      filename:
        label: Dateiname ohne Endung
        type: text

and here the one for the foreach:

<ul>
<?php foreach ($galleryfiles as $galleryfile): ?>
<li>
<a href="#<?php echo $galleryfile['filename']; ?>" title="<?php echo $galleryfile['text']; ?>">
<?php if($image): ?>
<?php echo thumb($page->image(), array('width'=>300, 'crop'=>true)); ?>
<?php endif ?>
</a>
</li>
<?php endforeach ?>
</ul>

Yes, but I removed my response again, because I wasn’t sure what sort of field you used, but it was in fact correct:

In the same way that you call the filename in your href attribute by its index, you have to do the same with the picture field:

<?php foreach ($galleryfiles as $galleryfile): ?>
  <?php if($image = $page->image($galleryfile['picture'])): ?>
    <?php echo thumb($image, array('width'=>300, 'crop'=>true)); ?>
  <?php endif ?>
<?php endforeach ?>
```

Pls. To improve code readability, pls. use three backticks on a separate line both before and after a block of code. Helps a lot. Thank you.:slight_smile:

Thanks a lot!
With your help it works now.

Thank you also for the hint with the three ticks on a separate line. I just used two for each line …

Johannes