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.

  • You don't have sub categories, do you? I'm wondering what you're trying to achieve with the categoryToDisplay var... – carlcs Aug 15 '14 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 Poirier Aug 15 '14 at 11:23
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 %}
  • Thank you very much for the tips, and I can confirm that this solution works perfectly! – Romain Poirier Aug 15 '14 at 11:37

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.