2

I'm struggling to figure out how to do this:

  • We have a bunch of entries in single a channel.
  • There are two category groups an entry can be assigned to, with multiple categories in each group.

We want to show all entries, but grouped in a nested format as follows:

  • Loop through Category Group One and get the entries grouped by each category.
  • Within each Group One category, loop through Category Group Two, to output entries grouped by Category Group Two categories.

I've found Relations hard enough to wrap my head around when I'm passing just a single category, but this has melted my mind completely.

For more specifics:

  • Category Group One has categories which are days of the week: Mon, Tue, Wed, Thus, Fri, Sat, Sun.
  • Category Group Two has categories that are activities: Walking, Running, Skiing

The output ought to be grouped something like:

Mon - Walking [entry title, entry title, entry title]
Mon - Running [entry title]
Mon - Skiing  [entry title, entry title]
Tue - Walking [...]
Tue - Running [...]
...

This is the code I've got closest with, but it's outputting duplicate entries within each day group...

{% set days       = craft.categories.group('day') %}
{% set activities = craft.categories.group('activity') %}

{% for day in days %}
    <li>
        {{ day.title }}
        {% for activity in activities %}
            {{ activity.title }}
            {% for entry in craft.entries.section('attractions').relatedTo(
                'and',
                { targetElement: day },
                { targetElement: activity }
            ) %}
                    {% if loop.first %}<ul>{% endif %}
                    <li><a href="{{ entry.url }}">{{ entry.title }}</a></li>
                    {% if loop.last %}</ul>{% endif %}
            {% endfor %}
        {% endfor %}
    </li>
{% endfor %}
3

Finally figured it out, I had been so close! The following works:

{% set days       = craft.categories.group('day') %}
{% set activities = craft.categories.group('activity') %}

    {% for day in days %}
        <h2>{{ day.title }}</h2>

        {% for activity in activities %}
            {% set entries = craft.entries.section('attractions').relatedTo(
              'and',
              { targetElement: day },
              { targetElement: area }
            ) %}

            {% if entries | length %}
                <h3>{{ area.title }}</h3>

                {% for entry in entries %}
                    <p><a href="{{ entry.url }}">{{ entry.title }}</a> {{ entry.startTime|date('H:i') }}</p>
                {% endfor %}
            {% endif %}
        {% endfor %}
    {% endfor %}
1

Finally figured it out, I had been so close! The following works:

{% set days       = craft.categories.group('day').slug(abc) %}
{% set activities = craft.categories.group('activity').slug(xyz) %}

{% for day in days %}
    <h2>{{ day.title }}</h2>

    {% for activity in activities %}
        {% set entries = craft.entries.section('attractions').relatedTo(
          'and',
          { targetElement: day },
          { targetElement: area }
        ) %}

        {% if entries | length %}
            <h3>{{ area.title }}</h3>

            {% for entry in entries %}
                <p><a href="{{ entry.url }}">{{ entry.title }}</a> 
                {{ entry.startTime|date('H:i') }}</p>
            {% endfor %}
        {% 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.