Answers.Body: <p>Try this:</p>
  
  <pre><code>{% for category in categories %}
      &lt;h2&gt;{{ category.title }}&lt;/h2&gt;{# spit out the Category #}
  
      {% set entries = craft.entries.section('news').relatedTo(category) %}
      {% for relatedEntry in entries %}
          {{ relatedEntry.title }}
      {% endfor %}
  {% endfor %}
  </code></pre>
  
  <p>See the set entries = ... line.</p>
  
  <p>I am retrieving the entries that are related to each category as it loops thru each category.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-24T16:08:56.657Z
Answers.LastActivityDate: 2017-04-24T21:56:25.983Z
Answers.LastEditDate: 2017-04-24T21:56:25.983Z
Answers.UserDisplayName: Brad
Body: <p>Hope somebody can help us with this: </p>
  
  <p>Objective: loop through a given Entry's "Related Entries" entry field and output the results - but group the results by the category to which they belong - not just a straight splurge of related entries.</p>
  
  <p>Where we are:</p>
  
  <pre><code>{% for category in categories %}
  &lt;h2&gt;{{ category.title }}&lt;/h2&gt;{# spit out the Category #}
  
  {% for relatedEntry in entry.relatedEntries %}
      {{ relatedEntry.title }}{# this spits out all entries, but ignores whether the entry is related to the current category #}
  
      {# So, try and check if it's related to the current category... #}
      {% if relatedEntry.relatedTo(category) %}{# Trying to use this throws "Craft\EntryModel and its behaviors do not have a method or closure named "relatedTo"." #}
          &lt;p&gt;{{ relatedEntry.title }}&lt;/p&gt;
      {% endif %}
  {% endfor %}
  {% endfor %}
  </code></pre>
  
Tags: <entries><category><relatedto>
Title: loop through an entry's "related entries", grouped by category
ThreadText: # loop through an entry's "related entries", grouped by category
  
  (Asked by: username_0 on 2017-04-24)
  
  Hope somebody can help us with this:
  
  Objective: loop through a given Entry's "Related Entries" entry field and output the results - but group the results by the category to which they belong - not just a straight splurge of related entries.
  
  Where we are:
  
  ```
  {% for category in categories %}
  <h2>{{ category.title }}</h2>{# spit out the Category #}
  
  {% for relatedEntry in entry.relatedEntries %}
      {{ relatedEntry.title }}{# this spits out all entries, but ignores whether the entry is related to the current category #}
  
      {# So, try and check if it's related to the current category... #}
      {% if relatedEntry.relatedTo(category) %}{# Trying to use this throws "Craft\EntryModel and its behaviors do not have a method or closure named "relatedTo"." #}
          <p>{{ relatedEntry.title }}</p>
      {% endif %}
  {% endfor %}
  {% endfor %}
  
  ```
  
  **Tags:** entries, category, relatedto
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-04-24. Score: 2
  
  Try this:
  
  ```
  {% for category in categories %}
      <h2>{{ category.title }}</h2>{# spit out the Category #}
  
      {% set entries = craft.entries.section('news').relatedTo(category) %}
      {% for relatedEntry in entries %}
          {{ relatedEntry.title }}
      {% endfor %}
  {% endfor %}
  
  ```
  
  See the set entries = ... line.
  
  I am retrieving the entries that are related to each category as it loops thru each category.
  
  
  ---
  
  