2

Here's a summary of what I try to do:

  1. I created a Single page with a specific template where I want to display a list of partners
  2. I created a group of Categories to define all types of partners
  3. All my partners are encoded in a 'partners' Channel
  4. In this Channel, each partner has a field that allows to select one partner Category
  5. On the Single page, I display all categories of partners with at least one partner
  6. 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.

2
  • You don't have sub categories, do you? I'm wondering what you're trying to achieve with the categoryToDisplay var...
    – carlcs
    Commented Aug 15, 2014 at 11:16
  • No, in my Category group ‘Partners’ I’ve only Categories, no sub categories. With ‘categoryToDisplay’ I’m trying to display each entry related to the current Category displayed. I think I’m totally wrong here but frankly don’t know how to do :-/
    – Romain P.
    Commented Aug 15, 2014 at 11:23

1 Answer 1

4

Here's the link to relevant the "Get Help" article:

How do I list the entries related to a category?

These really helpful tutorials are somewhat hidden on builtwithcraft.com, I overlooked them more than once while thought the normal Documentation articles!

Just use the provided example code in your category loop:

{% 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>
        <ul>     

            {# Fetch all of the entries related to this category #}
            {% set entries = craft.entries.relatedTo(category) %}

            {# Output them #}
            {% for entry in entries %}
                <li>{{ entry.title }}</li>
            {% endfor %}

        </ul>
    </div>
{% endfor %}
2
  • Thank you very much for the tips, and I can confirm that this solution works perfectly!
    – Romain P.
    Commented Aug 15, 2014 at 11:37
  • Hi, I know this is a very old question, and I would do it the same way. Performance wise: Do you know a way to overcome the N+1 problem? Scaling this approach would cause performance problems., wouldn't it?
    – outline4
    Commented Feb 25, 2022 at 10:18

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.