I have a section called reviews and a section called games. A entry field in games is creating a relationship to reviews.

On my _entry.html entry for reviews, I want to link back to the related game.

My code is below. I understand why it isn't working, but not sure how to fix it.

The template displays the Review entry title, the body some extra fields in reviews.

And then gets to the Game Link, but the link is not to my Games section but to my Reviews section.

In other words, on this review: http://www.colorcomputergames.com/reviews/1984-01-01/glaxxons

The link at the bottom should be to: http://www.colorcomputergames.com/games/glaxxons

But instead, goes to: http://www.colorcomputergames.com/reviews/1984-01-01/glaxxons

<h1>{{ entry.title }}</h1>
    {% if entry.ReviewsAuthor|length %}<li>Reviewer: {{ entry.ReviewsAuthor }}</li>{% endif %}
    {% if entry.ReviewsMedia|length %}<li>Media: {% for category in entry.ReviewsMedia %}<a href="{{ category.url }}">{{ category.title }}</a>{% endfor %}</li>{% endif %}
    {% if entry.ReviewsDate|length %}<li>Date: {{ entry.ReviewsDate.format('F Y') }}</li>{% endif %}
    {% if entry.ReviewsRelatedLink|length %}<li>Related Link: {{ entry.ReviewsRelatedLink }}</li>{% endif %}
{{ entry.body }}

{% set entries = craft.entries.section('games').relatedTo(entry) %}
<h3>Game Link:</h3>
        <li><a href="{{ entry.url }}" title="{{ entry.title }}">{{ entry.title }}</a></li>

As mentioned, in section called Games, I have a field called GamesReview and set it to entries and assign the review entry. Do I need to do the opposite as well t make this work? That is, in my reviews section, create a field called GameReviewed and set it to entries and assign it to Games section.



Your code isn't working because you set the related game(s) to a variable entries (i.e. {% set entries = craft.entries...%}), but you're not actually doing anything with that variable. Then you try to render the link to the related game(s), but you're simply re-using the entry variable – which refers to the review entry, not the related game entry.

If you change

{% set entries = craft.entries.section('games').relatedTo(entry) %}
<h3>Game link:</h3>

to something like

{% set relatedGames = craft.entries.section('games').relatedTo(entry) %}
{% if relatedGames|length %}
    <h3>Game Link:</h3>
        {% for game in relatedGames %}
            <li><a href="{{ game.url }}" title="{{ game.title }}">{{ game.title }}</a></li>
        {% endfor %}
{% endif %}

...it should work.

Note that in some cases, its a good idea to be as specific as possible when you're dealing with relations. Simply doing relatedTo(entry) will work, but you can also let Craft know where the relation was created (i.e. in which field), and whether the related elements you're looking for is the source or the target elements for the relation. In this case, then Entries field is on the game entry, which means that the review entry is the target element (i.e. the entry that has been selected in an Entries field), and the game entry is the source element (i.e. the entry that actually has the Entries field in its field layout).

Assuming your Entries field on the game entry has a handle relatedReviews, a more specific relatedTo query for your use case could look like this:

{% set relatedGames = craft.entries.section('games').relatedTo({
    field: 'relatedReviews',
    targetElement: entry
}) %}

Being specific about relations can be more performant, and helps avoid issues where you might have multiple relations involving the same elements (e.g. if your review and game entries where also related via a different Entries field).

Another detail to note is that whenever you're dealing with the source element (e.g. if this was the template for the game entry, and you wanted to list the related review entries), you rarely need relatedTo – you can just do this (of course, replace relatedReviews with the real handle for your Entries field):

{% set relatedReviews = entry.relatedReviews %}
{% for review in relatedReviews %}
    <a href="{{ review.url }}">{{ review.title }}</a>
{% endfor %}
  • Your code worked perfect, thanks! I do have related reviews listed on the game entry templates. I am doing it in the following way but would love to know if my code is less-efficient than yours: {% set reviews = entry.GamesReviews %}{% if reviews|length %}<h3>Reviews:</h3><ul>{% for review in reviews.order('title asc') %} <li><a href="{{ review.url }}" title="{{ review.title }}">{% for category in review.ReviewsMedia %}{{ category.title }}{% endfor %} - {{ review.title }}</a></li>{% endfor %}</ul>{% endif %} – DevServe Dec 29 '16 at 0:55

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.