Display page's second image only

I am trying to display the second image from the page. Without the offset it displays the first image without issue, but I am trying to skip that one and display just the second image. With the offset included nothing is displayed. See code below.

<?php if($image = $page->image()->offset(1)): ?>
    <img src="<?= $image->resize(1280)->url() ?>" id="expandedImg">
<?php endif ?>

offset() is a collection method, but you are calling it on a single image $page->image(). The correct code would be:

<?php if($image = $page->images()->offset(1)): ?>
    <img src="<?= $image->resize(1280)->url() ?>" id="expandedImg">
<?php endif ?>

Thanks for your quick response! When I plug this in it breaks the page and I get a page offline error; there is for sure a second image file in the page. Is there a better way to display second image than using offset method?

Sorry, I didn’t pay close attention, offset() is the wrong method, as it also returns a collection, not a single file. nth(2) would be the correct method.

I’d recommend enabling debugging in your config, to get proper error messages. Also, the documentation indicates a return type for every method, so you know what you get.

This worked, thank you! Will do. Here is the final code for ref:

<?php if($image = $page->images()->nth(1)): ?>
    <img src="<?= $image->resize(1280,1000)->url() ?>" id="expandedImg">
<?php endif ?>