How to fix broken image path when using toStructure()?

Alt text and caption can be a bit redundant depending on how you use them. I think the alt text is meant to be very short and descriptive while a caption can provide additional information. There are lots of interesting reads online (I just spent 25 minutes reading through a few) :wink:

Here’s the full code for you. I’m using an if statement to verify that the image exists, to avoid throwing errors.

<section class="gallery">
    <?php foreach($page->gallery()->toStructure() as $pic): ?>
      <?php if($img = $pic->gallery_image->toFile()): ?>
      // If the image exists, create a figure
      <figure class="gallery-item one-third-offset">
        <img src="<?php echo $img->url() ?>" alt="<?php echo html($img->title()) ?>">
        <figcaption><?php echo html($pic->caption()) ?></figcaption>
      </figure>
      <?php endif ?>
    <?php endforeach ?>
</section>

Note 1: You don’t need a structure field if you’re only going to use the images, you could store the caption along the file. See this for reference: https://getkirby.com/docs/panel/blueprints/file-settings#file-fields
You could then use another simpler fields for your needs (I think the gallery field is great for this: Kirby Gallery Field)

Note 2: If you still want to use recipes. There’s a great recipe in the cookbooks that’s all about structure fields. It’s a must read :wink: https://getkirby.com/docs/cookbook/the-structure-field