Looping through images inside structure field

I am having issues with rendering my images through the structure field

Here is my blueprint:

fields: 
  archive: 
    label: Archive Structure
    type: structure
    columns: 
      image: 
        width: 3/5
    fields: 
      image:
        type: files
        query: page.images
        uploads: true


<?php 
    $items = $page->archive()->toStructure();

    foreach ($items as $item): ?>
        <div class="archive__item gc">
            <p class="span-3"><?= $item->project()->html() ?></p>
            <p class="span-3"><?= $item->type()->html() ?></p>
            <p class="span-3"><?= $item->year()->html() ?></p>
            <?php foreach ($item->images()->toFiles() as $image): ?>
                <img class="span-3" src="<?= $image->crop(400)->url() ?>">
            <?php endforeach ?>
        </div> 
    <?php endforeach ?>

Any suggestions?

Looks like a typo :slight_smile:

Since the name of the field is image, this should be:

$item->image()->toFiles()

…or you change the field name to images in the blueprint, which is more semantic if you are dealing with more than one file.

1 Like