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 %}