1

I am trying to loop through an assets field called gallery on entries in a commerce products channel.

This field can have multiple images in it. I can get this to work on a channel listing page domain.com/shop/ but can't get this to work on the entry detail page domain.com/shop/products/detail/.

Any help is appreciated.

{% for asset in product.gallery.first() %}
    <img src="{{ asset.url }}">
{% endfor %}
  • I edited your question just to clarify some of the naming - the gallery field is on entries in a commerce products channel. – Jeremy Daalder Jul 18 '16 at 1:14
3

product.gallery returns an ElementCriteriaModel, which you can think of as a list of elements. product.gallery.first() will return the first element in this list.

What you want is to loop over all the elements:

{% for asset in product.gallery %}
  <img src="{{ asset.url }}">
{% endfor %}

(Note that this will work if there are no elements in the list (because the for loop will never be entered). One thing you should avoid doing is product.gallery.first().url, because it is never possible to be certain without checking that there is a first element in the list.)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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