How to show multiple images inside foreach?

Hi, I am showing projects with this code:

<?php
foreach($site->find('projects')->children()->filterBy('tipas', 'metal', ',') as $project):
?>
.....
<?php endforeach ?>

Blueprint of project has field called gallery with images:

          gallery:
            headline: Images
            type: files
            layout: cards
            template: image
            size: tiny
            image:
              ratio: 5/4
              cover: true

I had no problem to show one (cover) image, but can’t understand, how to show multiple images, for example three first. Tried this code inside main loop:

<?php foreach($project->gallery()->toFiles() as $img): ?>
        <?= $img->Crop(200, 200) ?>
<?php endforeach ?>

Goal is to have slides here instead of one image http://metalista.lt.grikis.serveriai.lt/lt/projects
slideshow

Oh, I found solution by looking into another analogical template :slight_smile:

This works fine:

<?php foreach ($project->images() as $img): ?>
        <?= $img ?>
<?php endforeach ?>

Only curious why I get gallery field file with images()? And as I understand in Kirby 3.2 it should be different?

I don’t understand your question?

I would expect to show gallery field this way:

$project->gallery(), but instead they are showing with $project->images()

$project->images() fetches all images from a page. $project->gallery() doesn’t make sense. If you want to fetch images from a files field called gallery you have two possibilities:

// single image
$image = $project->gallery()->toFile();

//multiple images
$images = $project->gallery()->toFiles();
1 Like

Ah, ok, I wrote not full code in last comment, just was making accent on field title gallery, instead of images. Now I understand, thanks.

Like I wrote in post, tried this method $images = $project->gallery()->toFiles();, but could not get images this way. Will try one more time and see. But in this certain case $project->images() is also ok, as there are no other image fields.

In that case, a files section would probably make more sense, because the files field allows you to select images. So if the user selects only part of the uploaded images and you output all images, that sort of defeats the purpose of the files field.

In this case we need to show all images from this one field, so it’s fine.

Still don’t understand how to get images from this field. Is something wrong with this code?

<?php foreach($project->gallery()->toFiles() as $img): ?> 
 <?= $img->Crop(200, 200) ?> 
<?php endforeach ?>

it doesn’t output anything.

Looking at your blueprint above, gallery is not a field but a section. So $page->images()->filterBy('template', 'image') would be the correct code.

I see, thank you. Trying to understand logic behind it. For example if I change in section blueprint to this:

      gallery:
        headline: Images
        type: files
        layout: cards
        template: anothertemplate
        size: tiny
        image:
          ratio: 5/4
          cover: true

I still get the same images with filterBy(‘template’, ‘image’), although different template is set.

yes, because your change will only affect newly uploaded files. However, if you change the template, you will not longer see files with the image template in your section in the Panel.

When you upload a file with a template set, this information is stored in the meta data file for each file.

Clear now, thanks! And indeed, don’t see files in panel now :slight_smile: