Add a caption to gallery block

Hello community,

I’m looking for a simple way to add a figcaption to the gallery block, similar to the image block, but for all gallery items. Right now I’m only able to add a field to the gallery and not to all individual images within the gallery.

Hello, you can add caption for files, and use it in templates

Hey…

I m a little bit closer. I added to my files blueprint the caption like this:

title: Image

accept:
  mime: application/pdf, image/jpeg, image/png, image/svg+xml

fields:
  alt:
    label: Alt Text
    type: text
    width: 1/1
  caption:
    label: Bild Beschreibung
    type: text

and in the gallery.yml I added this:

name: field.blocks.gallery.name
icon: dashboard
preview: gallery
fields:
  images:
    label: field.blocks.gallery.images.label
    type: files
    multiple: true
    layout: cards
    size: tiny
    empty: field.blocks.gallery.images.empty
    uploads:
      template: blocks/image
    image:
      ratio: 1/1
    info: "{{ file.caption }}"

but now, how do I get the caption to the template?

Ahhhh… I got it!

<?php /** @var \Kirby\Cms\Block $block */ ?>
<div class="gallery">
    <ul class="gallery__list">
        <?php foreach ($block->images()->toFiles() as $image): ?>
        <li>
            <figure>
                <?= $image ?>
            </figure>
            <?= $image->caption() ?>
        </li>
        <?php endforeach ?>
    </ul>
</div>
2 Likes