1

This is what I'm trying to accomplish:

  • All categories displayed on one page, with entries returned from each.
  • Only six entries shown in each category.
  • Categories need to be in a specific order.

I've got the first two points working, but can't figure out how to change the order of the categories. Am I going about this the wrong way?

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

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

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

    {% endfor %}

{% endfor %}
2

Using the group filter you will always end up with the sort order of your entries. So you would need to sort the entries by your categories field, but this probably doesn't work.

But you can do the following:

{# Get the categories related to my "entrySection" entries in custom order #}
{% set entries = craft.entries.section('entrySection') %}
{% set relatedCats = craft.categories.relatedTo(entries).order('customCatField desc') %}

{# Loop the categories #}
{% for category in relatedCats %}

    {# Get and loop through the 6 most recent entries related to this cat #}
    {% set relatedEntries = craft.entries.relatedTo(category).limit('6') %}
    {% for entry in relatedEntries %}
        ...
    {% endfor %}

{% endfor %}
  • For whatever reason, putting a category field into the order function threw errors, but I was able to get it working by using dateUpdated instead. Otherwise this worked perfectly! – brandongcarroll Dec 4 '14 at 17:52
  • With "customCatField" I actually meant a custom field assigned to your categories (e.g. category color)! Glad it worked for you :) – carlcs Dec 4 '14 at 18:34
  • Thank you @carlcs I had looked at that too, but yes that does exactly what I need after actually implementing it :) – Gareth Dec 8 '14 at 14:03

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.