3

Ok, so basically I want to loop through categories and output the title of each category along with all the associated entries. Like so:

Category 1: Entry 1 Title, Entry 3 Title

Category 2: Entry 2 Title, Entry 4 Title

(Where entry 1 and 3 are category 1 - and entry 2 and 4 are category 2)

This was the closest I got:

    {% set categories = craft.categories.group('jobCategories').find() %}

        {% for category in categories %}

            <h1>{{ category.title }}</h1>

            {% for entry in craft.entries.section('people').relatedTo(category).find() %}

                <a href="{{ entry.url }}">
                     {{ entry.title }}
                </a>

            {% endfor %}

    {% endfor %}

How would I do this?

Thanks!

1 Answer 1

8

It might be quicker to turn it around: first get all the entries you need, then group those by their category:

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

{% for category, catEntries in entries | group('categoryFieldHandle.first().title') %}
    <h1>{{ category }}</h1>
    {% for entry in catEntries %}
        <a href="{{ entry.url }}">{{ entry.title }}</a>
    {% endfor %}
{% endfor %}
6
  • Ok, just tried that and all it's doing is outputting the section name.
    – Lighty_46
    Commented Sep 25, 2014 at 15:19
  • Try putting .find() behind the first rule.
    – Paul
    Commented Sep 25, 2014 at 15:22
  • Hmmmm, nope still does the same thing.
    – Lighty_46
    Commented Sep 25, 2014 at 15:33
  • Wait, it works now for some reason. I used: {% set entries = craft.entries.find({ section: 'people'}) %} in the end. Thanks for the help!
    – Lighty_46
    Commented Sep 25, 2014 at 15:36
  • Strange, I would think that those two things have the same result ..
    – Paul
    Commented Sep 25, 2014 at 15:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.