Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-15T11:37:13.773Z
Answers.Text: Thank you very much for the tips, and I can confirm that this solution works perfectly!
Answers.UserDisplayName: Romain P.
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-02-25T10:18:23.757Z
Answers.Text: 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?
Answers.UserDisplayName: outline4
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-15T11:29:51.780Z
Answers.LastActivityDate: 2014-08-15T11:29:51.780Z
Answers.UserDisplayName: carlcs
Body: <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>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-08-15T11:16:43.350Z
Comments.Text: You don't have sub categories, do you? I'm wondering what you're trying to achieve with the `categoryToDisplay` var...
Comments.UserDisplayName: carlcs
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-08-15T11:23:35.257Z
Comments.Text: 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 :-/
Comments.UserDisplayName: Romain P.
Tags: <forloop><relations><entry><category>
Title: How do I create a list of categories and display the entries relating to each of these categories that have at least one entry?
ThreadText: # 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:
  
  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.
  
  **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?](http://buildwithcraft.com/help/entries-related-to-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 %}﻿
  
  ```
  
  > **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?  
  
  ---
  
  