<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>

CC BY-SA 3.0
2017-04-24T16:08:56.657Z
2017-04-24T21:56:25.983Z
2017-04-24T21:56:25.983Z
Brad
<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>

<entries><category><relatedto>
loop through an entry's "related entries", grouped by category
# 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 %}
{{ category.title }}{# 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"." #}
        {{ relatedEntry.title }}
    {% 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 %}
    {{ category.title }}{# 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.


---

