1

In craft cms 3 we could use this:

{{ entry.images[1].url }}

But how can I get second asset in Craft CMS 4?

1 Answer 1

2

You need to execute the element query, then access the second element of the resulting array:

{% set secondImage = entry.images.all()[1].url ?? null %}
{% if secondImage %}
    {# … #}
{% endif %}

The null coalescing operator (??) is useful here to prevent errors in case there aren't two images selected in the images field.

3
  • 1
    Just need to replace images(). by images. Mar 14, 2023 at 16:27
  • I can't edit it as stackexchange demand at least 6 changed characters :) Mar 14, 2023 at 16:28
  • @NikolayBronskiy Ah yes, good catch, slip of the pen :)
    – MoritzLost
    Mar 14, 2023 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.