2

I'm wondering how I would go about limiting the number of results returned from a set using the group filter? I only want four to show, but using the limit() method seems to throw errors wherever I put it. Here's my template code:

{% set entries = craft.entries.section('recipes').find() %}

{% for category, catEntries in entries | group('cuisineType.first().title') %}

    {% for entry in catEntries %}

       // I only want four entries shown here

    {% endfor %}

{% endfor %}
5

The reason limit() isn't working is that is a method on the ElementCriteriaModel you get from craft.entries.

Because you're actually dealing with a standard array when you access catEntries, what you want instead is Twig's |slice(start, length) filter:

{% for entry in catEntries|slice(0, 4) %}

   // You'll only get four entries shown here

{% endfor %}
| improve this answer | |
  • 1
    Worth mentioning, there is a shorthand for the slice filter... catEntries[0:4] – Lindsey D Nov 28 '14 at 20:52
  • This worked flawlessly. Now I'm wondering if there's a way to order the categories that are returned, entries can come in any order, but I'd like my categories to output in a particular one here? – brandongcarroll Dec 4 '14 at 2:46

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.