Here's a summary of what I try to do:
- I created a Single page with a specific template where I want to display a list of partners
- I created a group of Categories to define all types of partners
- All my partners are encoded in a 'partners' Channel
- In this Channel, each partner has a field that allows to select one partner Category
- On the Single page, I display all categories of partners with at least one partner
- For each Category of partners, I display the partner(s) related to this Category
And here is my code which attempt to do this, but not correctly:
{% set entryIds = craft.entries.section('partners').ids() %}
{% set categories = craft.categories.relatedTo({ sourceElement: entryIds }).groupId(2).find() %}
{% for category in categories %}
<div>
<h3>{{ category.title }}</h3>
{% set categoryToDisplay = category.slug %}
{% for entry in craft.entries.section('partners') %}
<ul>
{% for category in craft.categories(categoryToDisplay) %}
<li>{{ entry.title }}</li>
{% endfor %}
</ul>
{% endfor %}
</div>
{% endfor %}
This properly displays all Partners categories with at least one partner, but this does not correctly display the partners associated with each category. Each time, all partners of all categories are repeated instead of displaying only those that are related.
categoryToDisplay
var...