Previous and Next image in a foreach loop

I have a very simple setup but can’t seem to work out the syntax:

<?php foreach($images->sortBy('sort','asc') as $image): ?>
  <div class="current">
    <img src="<?= $image->url() ?>" alt="">
    <h3 style="color:<?= $image->colour()->html() ?>;">&lsquo;<?= $image->title()->html() ?>&rsquo;</h3>
  </div>
  <div class="prev-next">
    <div class="prev">
      <h3 style="color:<?= $image->colour()->html() ?>;"><?= $image->title()->html() ?></h3>
    </div>
    <div class="next">
      <h3 style="color:<?= $image->colour()->html() ?>;"><?= $image->title()->html() ?></h3>
    </div>
  </div>
<?php endforeach ?>

In the ‘prev’ and ‘next’ divs I would like to get the ‘title’ and ‘colour’ values stored with the previous and next images in the loop. I tried many combinations of ‘next->’ but couldn’t get it to work.

Thanks for any help!

You can use prev() and next()

https://getkirby.com/docs/cheatsheet/file/next
https://getkirby.com/docs/cheatsheet/file/prev

1 Like

I can’t seem to get it to work in my context

<?= $image->next->title()->html() ?>

I’m getting ‘Call to a member function title() on boolean’ etc…

Have you setup meta data for the images? You are getting that error because it is looking for a title field in the meta data for the image, and it cannot find it. You can learn about metadata here.

Where and how are you setting $images? It’s not clear if you are working with images directly or getting the images from pages in the foreach loop.

Yes, I have meta data for the images. I’m getting the images like this:

   <?php
      $images = $page->images();
   ?>

Then looping through them with the above code. The current image and metadata are all loaded fine in the loop, I just cannot get next and prev to work in any context.
Thanks!

Hint:
in the code part

check, whether the other files realy exist.

1 Like

You have a typo here, is that correct in your code next() not next, i.e. with the parenthesis.

And additionally, you should always check if the image actually exists, like in the examples I linked to. For example, your first image in the loop will never have a previous image and the last one never a next.

2 Likes

Just to clear this up: The error message “Call to a member function title on boolean” or similar messages never appears because of a missing title or meta data but because of a missing object, that means when $image->next() returns false instead of a file object. That is because you can only ever call a class method on an instance of that class.

1 Like

I added an ‘if’ and it now works, so thanks everyone! Seems odd that it doesn’t work without the ‘if’ even if a ‘next’ image definitely exists.

Well, as I explained above, the first image doesn’t have a previous file and the last one doesn’t have a next file; since you are doing this within a loop, the first file will definitely throw an error when calling prev() and the last one when calling next().

If you want to have an infinite loop, you have to use a custom function.

sorry, as @texnixe said, you need two if’s! One for the next part and one for the prev part.