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.
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!
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.
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.
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.