Answers.Body: <p>This will exclude empty categories, meaning they don't have any entries associated with them.</p>
  
  <pre><code>{% set entryIds = craft.entries.section('channelHandle').ids() %}
  {% set categories = craft.categories.relatedTo({ sourceElement: entryIds }).group('categoryGroupHandle') %}
  
  {% for category in categories %}
      &lt;h2&gt;{{ category.title }}&lt;/h2&gt;
      &lt;ul&gt;
          {% set entries = craft.entries.section('channelHandle').relatedTo(category) %}
          {% for entry in entries %}
              &lt;li&gt;&lt;a href="{{ entry.url }}"&gt;{{ entry.title }}&lt;/a&gt;&lt;/li&gt;
          {% endfor %}
      &lt;/ul&gt;
  {% endfor %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-09-14T02:22:34.753Z
Answers.LastActivityDate: 2017-09-14T02:22:34.753Z
Answers.UserDisplayName: Ryan
Body: <p>I have a number of entries that all belong under a Channel that I created. Each entry has a category field where the user can select one or many categories to associate the entry with. </p>
  
  <p>I am displaying these entries on my page, but would like to display each entry under the appropriate category.</p>
  
  <p>For example, my entries are currently displayed like this:</p>
  
  <ul>
  <li>Entry 1 (Category A)</li>
  <li>Entry 2 (Category C)</li>
  <li>Entry 3 (Category A)</li>
  <li>Entry 4 (Category B)</li>
  <li>Entry 5 (Category A)</li>
  </ul>
  
  <p>I want it to display like this:</p>
  
  <ul>
  <li>Entry 1 (Category A)</li>
  <li>Entry 3 (Category A)</li>
  <li>Entry 5 (Category A)</li>
  <li>Entry 4 (Category B)</li>
  <li>Entry 2 (Category C)</li>
  </ul>
  
  <p>What's the best way to do this?</p>
  
Tags: <entries><categories><category>
Title: List entries by category
ThreadText: # List entries by category
  
  (Asked by: username_0 on 2017-09-14)
  
  I have a number of entries that all belong under a Channel that I created. Each entry has a category field where the user can select one or many categories to associate the entry with.
  
  I am displaying these entries on my page, but would like to display each entry under the appropriate category.
  
  For example, my entries are currently displayed like this:
  
  * Entry 1 (Category A)
  * Entry 2 (Category C)
  * Entry 3 (Category A)
  * Entry 4 (Category B)
  * Entry 5 (Category A)
  
  I want it to display like this:
  
  * Entry 1 (Category A)
  * Entry 3 (Category A)
  * Entry 5 (Category A)
  * Entry 4 (Category B)
  * Entry 2 (Category C)
  
  What's the best way to do this?
  
  **Tags:** entries, categories, category
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-09-14. Score: 2
  
  This will exclude empty categories, meaning they don't have any entries associated with them.
  
  ```
  {% set entryIds = craft.entries.section('channelHandle').ids() %}
  {% set categories = craft.categories.relatedTo({ sourceElement: entryIds }).group('categoryGroupHandle') %}
  
  {% for category in categories %}
      <h2>{{ category.title }}</h2>
      <ul>
          {% set entries = craft.entries.section('channelHandle').relatedTo(category) %}
          {% for entry in entries %}
              <li><a href="{{ entry.url }}">{{ entry.title }}</a></li>
          {% endfor %}
      </ul>
  {% endfor %}
  
  ```
  
  
  ---
  
  