<p>You could introduce a counter and count the images actually displayed in your nested for loops. Then add a conditional to the loops to stop the iteration at your set limit.</p>

<p>With that many parameters set on your <code>craft.entries</code>
<a href="http://buildwithcraft.com/docs/templating/elementcriteriamodel" rel="nofollow">ElementCriteriaModel</a> I would really recommend to use the <a href="http://buildwithcraft.com/docs/templating/elementcriteriamodel" rel="nofollow">object syntax</a> to set them. And instead of using the <code>search</code> parameter to check if there's images set to your entries, you would better use <code>relatedTo</code> and filter by entries with relations to an image model.</p>

<pre><code>{% set assets = craft.assets.limit(null) %}

{% set entries = craft.entries({
    section: 'pages',
    type: 'events',
    eventDate: '&lt;= ' ~ now,
    relatedTo: assets,
    order: 'eventDate desc',
}) %}

&lt;ul&gt;
    {% set count = 0 %}
    {% for entry in entries if count &lt; 5 %}
        {% for image in entry.gallery if count &lt; 5 %}
            &lt;li&gt;&lt;img src="{{ image.getUrl('smallImage') }}"&gt;&lt;/li&gt;
            {% set count = count + 1 %}
        {% endfor %}
    {% endfor %}
&lt;/ul&gt;
</code></pre>

CC BY-SA 3.0
2015-04-21T12:20:40.627Z
Thanks, why would it be beneficial to use the object syntax? And why a relatedto?
John Macpherson
CC BY-SA 3.0
2015-04-21T12:25:28.317Z
Don't you think the object syntax is more readable with that many parameters? And `relatedTo` because that's what as assets field does under the hood, it relates assets to your entry. I wouldn't use search unless it's absolutely necessary, because it relies on a search index file that might be not up-to-date.
carlcs
CC BY-SA 3.0
2015-04-21T12:52:47.343Z
Oh yes it's certainly more readable i thought there might be a performance issue. Thanks for your help. Appreciated.
John Macpherson
CC BY-SA 3.0
2015-04-21T15:07:43.223Z
I believe you can also just use  `loop.index` which is auto-incremented.
Douglas McDonald
CC BY-SA 3.0
2015-04-21T15:09:11.610Z
@Douglas wouldn't the loop var be reset for each outer loop iteration?
carlcs
CC BY-SA 3.0
2015-04-21T15:22:29.923Z
@carlcs Just tested. You're right (as usual) — doesn't like `loop.index` in the conditional (throws error).
Douglas McDonald
CC BY-SA 3.0
2015-04-20T17:56:56.793Z
2015-04-21T06:41:49.160Z
2015-04-21T06:41:49.160Z
carlcs
<p>Something like this should work:</p>

<pre><code>{% for images in craft.entries.section('pages').type('events').order('eventDate desc').eventDate('&lt;=' ~ now).search('gallery:*') %}

    {% set image_set = images.gallery %}
    {% for i in 0..4 %}
        {% if image_set[i] is defined %}
            &lt;li&gt;&lt;a href="{{image_set[i].getUrl}}"&gt;&lt;img src="{{image_set[i].getUrl('smallImage')}}" alt="{{image_set[i].title}}" class="image-link" /&gt;&lt;/a&gt;&lt;/li&gt;
        {% endif %}
    {% endfor %}
{% endfor %}
</code></pre>

CC BY-SA 3.0
2015-04-20T17:39:47.623Z
2015-04-20T17:39:47.623Z
Aaron Berkowitz
<p>I'm trying to limit the total number of images shown to 5. The below clearly isn't correct but gives a rough idea of what i want to do.</p>

<p>One entry could contain 1 image and the next entry 3 images. So i can't set a limit on the craft.entries tag because of the unknown number of images in an entry. It's on the images for loop i want to limit to 5.</p>

<pre><code>{% for images in craft.entries.section('pages').type('events').order('eventDate desc').eventDate('&lt;=' ~ now).search('gallery:*') %}
    {% for image in images.gallery.limit(5) %}
        &lt;li&gt;&lt;a href="{{image.getUrl}}"&gt;&lt;img src="{{image.getUrl('smallImage')}}" alt="{{image.title}}" class="image-link" /&gt;&lt;/a&gt;&lt;/li&gt;
    {% endfor %}

{% endfor %}  
</code></pre>

<entries>
Limiting image rows by 5 but an unknown number of section entries
# Limiting image rows by 5 but an unknown number of section entries

(Asked by: username_0 on 2015-04-20)

I'm trying to limit the total number of images shown to 5. The below clearly isn't correct but gives a rough idea of what i want to do.
One entry could contain 1 image and the next entry 3 images. So i can't set a limit on the craft.entries tag because of the unknown number of images in an entry. It's on the images for loop i want to limit to 5.
{% for images in craft.entries.section('pages').type('events').order('eventDate desc').eventDate('<=' ~ now).search('gallery:*') %}
    {% for image in images.gallery.limit(5) %}
        
    {% endfor %}

{% endfor %}

**Tags:** entries

---

## 2 answers

### Answer by username_1 on 2015-04-20. Score: 1 | Accepted answer

You could introduce a counter and count the images actually displayed in your nested for loops. Then add a conditional to the loops to stop the iteration at your set limit.
With that many parameters set on your craft.entries
ElementCriteriaModel I would really recommend to use the object syntax to set them. And instead of using the search parameter to check if there's images set to your entries, you would better use relatedTo and filter by entries with relations to an image model.
{% set assets = craft.assets.limit(null) %}

{% set entries = craft.entries({
    section: 'pages',
    type: 'events',
    eventDate: '<= ' ~ now,
    relatedTo: assets,
    order: 'eventDate desc',
}) %}


    {% set count = 0 %}
    {% for entry in entries if count < 5 %}
        {% for image in entry.gallery if count < 5 %}
            
            {% set count = count + 1 %}
        {% endfor %}
    {% endfor %}

> **Comments:**
> **username_0**: Thanks, why would it be beneficial to use the object syntax? And why a relatedto?  
> **username_1**: Don't you think the object syntax is more readable with that many parameters? And `relatedTo` because that's what as assets field does under the hood, it relates assets to your entry. I wouldn't use search unless it's absolutely necessary, because it relies on a search index file that might be not up-to-date.  
> **username_0**: Oh yes it's certainly more readable i thought there might be a performance issue. Thanks for your help. Appreciated.  
> **username_2**: I believe you can also just use  `loop.index` which is auto-incremented.  
> **username_1**: @Douglas wouldn't the loop var be reset for each outer loop iteration?  
> **username_2**: @carlcs Just tested. You're right (as usual) — doesn't like `loop.index` in the conditional (throws error).  

---

### Answer by username_3 on 2015-04-20. Score: 3

Something like this should work:
{% for images in craft.entries.section('pages').type('events').order('eventDate desc').eventDate('<=' ~ now).search('gallery:*') %}

    {% set image_set = images.gallery %}
    {% for i in 0..4 %}
        {% if image_set[i] is defined %}
            
        {% endif %}
    {% endfor %}
{% endfor %}


---

