I’m looking for a way to let kirby count how many Images are in a foreach-loop and then display the specific number underneath them.
I’m using Kirby Blocks and some Blocks contain just text and one block contains an Image. But when an Image-Block is placed, I want to display the number under it in order to refer to them in a footnote.
Currently I’m displaying my blocks like this:
<?php foreach ($page->blocks()->toBlocks() as $block): ?> <div id="<?= $block->id() ?>" class="block block-type-<?= $block->type() ?>"> <?= $block ?> </div> <?php endforeach ?>
I’ve found something on the forum for a similar case and implemented it in my Image-Block-Snippet (although I think it’s completely wrong to place it into the block-template):
<figure> <ul> <?php $index = 0; foreach ($block->images()->toFiles() as $image): $index++; $next = $index + 1; ?> <li> Image <?php echo $index ?> <?= $image ?> </li> <?php endforeach ?> </ul> </figure>
Maybe someone could help me to use it the right way…
(In the end it should be possible to put Images between other blocks and the counting should work nevertheless)
Thanks in advance!