2

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 %}
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.