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) %}
        <li><a href="{{image.getUrl}}"><img src="{{image.getUrl('smallImage')}}" alt="{{image.title}}" class="image-link" /></a></li>
    {% endfor %}

{% endfor %}  

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 %}
            <li><img src="{{ image.getUrl('smallImage') }}"></li>
            {% set count = count + 1 %}
        {% endfor %}
    {% endfor %}
  • Thanks, why would it be beneficial to use the object syntax? And why a relatedto? Apr 21 '15 at 12:20
  • 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
    Apr 21 '15 at 12:25
  • Oh yes it's certainly more readable i thought there might be a performance issue. Thanks for your help. Appreciated. Apr 21 '15 at 12:52
  • I believe you can also just use loop.index which is auto-incremented. Apr 21 '15 at 15:07
  • @Douglas wouldn't the loop var be reset for each outer loop iteration?
    – carlcs
    Apr 21 '15 at 15:09

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 %}
            <li><a href="{{image_set[i].getUrl}}"><img src="{{image_set[i].getUrl('smallImage')}}" alt="{{image_set[i].title}}" class="image-link" /></a></li>
        {% endif %}
    {% endfor %}
{% endfor %}

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.