<p>Here's the link to relevant the "Get Help" article:</p>

<p><a href="http://buildwithcraft.com/help/entries-related-to-category" rel="nofollow">How do I list the entries related to a category?</a></p>

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

<p>Just use the provided example code in your category loop:</p>

<pre><code>{% set entryIds = craft.entries.section('partners').ids() %}
{% set categories = craft.categories.relatedTo({ sourceElement: entryIds }).groupId(2).find() %}

{% for category in categories %}
    &lt;div&gt;
        &lt;h3&gt;{{ category.title }}&lt;/h3&gt;
        &lt;ul&gt;     

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

            {# Output them #}
            {% for entry in entries %}
                &lt;li&gt;{{ entry.title }}&lt;/li&gt;
            {% endfor %}

        &lt;/ul&gt;
    &lt;/div&gt;
{% endfor %}﻿
</code></pre>

CC BY-SA 3.0
2014-08-15T11:37:13.773Z
Thank you very much for the tips, and I can confirm that this solution works perfectly!
Romain P.
CC BY-SA 4.0
2022-02-25T10:18:23.757Z
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
CC BY-SA 3.0
2014-08-15T11:29:51.780Z
2014-08-15T11:29:51.780Z
carlcs
<p>Here's a summary of what I try to do:</p>

<ol>
<li>I created a Single page with a specific template where I want to display a list of partners</li>
<li>I created a group of Categories to define all types of partners</li>
<li>All my partners are encoded in a 'partners' Channel</li>
<li>In this Channel, each partner has a field that allows to select one partner Category </li>
<li>On the Single page, I display all categories of partners with at least one partner</li>
<li>For each Category of partners, I display the partner(s) related to this Category</li>
</ol>

<p>And here is my code which attempt to do this, but not correctly:</p>

<pre><code>{% set entryIds = craft.entries.section('partners').ids() %}
  {% set categories = craft.categories.relatedTo({ sourceElement: entryIds }).groupId(2).find() %}
    {% for category in categories %}
      &lt;div&gt;
        &lt;h3&gt;{{ category.title }}&lt;/h3&gt;
        {% set categoryToDisplay = category.slug %}
        {% for entry in craft.entries.section('partners') %}
          &lt;ul&gt;
          {% for category in craft.categories(categoryToDisplay) %}
             &lt;li&gt;{{ entry.title }}&lt;/li&gt;
          {% endfor %}
          &lt;/ul&gt;
        {% endfor %}
      &lt;/div&gt;
    {% endfor %}﻿
</code></pre>

<p>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.</p>

CC BY-SA 3.0
2014-08-15T11:16:43.350Z
You don't have sub categories, do you? I'm wondering what you're trying to achieve with the `categoryToDisplay` var...
carlcs
CC BY-SA 3.0
2014-08-15T11:23:35.257Z
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.
<forloop><relations><entry><category>
How do I create a list of categories and display the entries relating to each of these categories that have at least one entry?
# How do I create a list of categories and display the entries relating to each of these categories that have at least one entry?

(Asked by: username_0 on 2014-08-15)

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 %}
      
        {{ category.title }}
        {% set categoryToDisplay = category.slug %}
        {% for entry in craft.entries.section('partners') %}
          
          {% for category in craft.categories(categoryToDisplay) %}
             {{ entry.title }}
          {% endfor %}
          
        {% endfor %}
      
    {% 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.

**Tags:** forloop, relations, entry, category
> **Comments:**
> **username_1**: You don't have sub categories, do you? I'm wondering what you're trying to achieve with the `categoryToDisplay` var...  
> **username_0**: 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 :-/  

---

## 1 answers

### Answer by username_1 on 2014-08-15. Score: 4 | Accepted answer

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 %}
    
        {{ category.title }}
             

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

            {# Output them #}
            {% for entry in entries %}
                {{ entry.title }}
            {% endfor %}

        
    
{% endfor %}﻿

> **Comments:**
> **username_0**: Thank you very much for the tips, and I can confirm that this solution works perfectly!  
> **username_2**: 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?  

---

