<p><code>product.gallery</code> returns an <a href="https://craftcms.com/docs/templating/elementcriteriamodel" rel="nofollow">ElementCriteriaModel</a>, which you can think of as a list of elements. <code>product.gallery.first()</code> will return the <a href="https://craftcms.com/docs/templating/elementcriteriamodel#first" rel="nofollow">first</a> element in this list.</p>

<p>What you want is to loop over all the elements:</p>

<pre><code>{% for asset in product.gallery %}
  &lt;img src="{{ asset.url }}"&gt;
{% endfor %}
</code></pre>

<p>(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 <code>product.gallery.first().url</code>, because it is <strong>never</strong> possible to be certain without checking that there <em>is</em> a first element in the list.)</p>

CC BY-SA 3.0
2016-07-18T00:44:50.703Z
2016-07-18T01:15:52.930Z
2016-07-18T01:15:52.930Z
Marion Newlevant
<p>I am trying to loop through an assets field called <code>gallery</code> on entries in a commerce <code>products</code> channel.</p>

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

<p>Any help is appreciated.</p>

<pre><code>{% for asset in product.gallery.first() %}
    &lt;img src="{{ asset.url }}"&gt;
{% endfor %}
</code></pre>

CC BY-SA 3.0
2016-07-18T01:14:53.227Z
I edited your question just to clarify some of the naming - the `gallery` field is on entries in a commerce products channel.
Jeremy Daalder
<assets><plugin-craftcommerce><loop>
Loop through assets field on detail/product page
# Loop through assets field on detail/product page

(Asked by: username_0 on 2016-07-16)

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() %}
    
{% endfor %}

**Tags:** assets, plugin-craftcommerce, loop
> **Comments:**
> **username_1**: I edited your question just to clarify some of the naming - the `gallery` field is on entries in a commerce products channel.  

---

## 1 answers

### Answer by username_2 on 2016-07-18. Score: 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 %}
  
{% 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.)


---

