1

I have two sections:

  • Games
  • Publishers

Games has a field called "GamesPublisher" that is a relation (field type "Entries"), the Source is "Publishers".

In my template to show an entry from games, I want to display the default field "title" of a publisher that I assigned to the games entry in the admin CP.

{% extends "_layout" %}
{% set title = "Games" %}
{% set entries = craft.entries.section('games').limit(10).order('title') %}
{% block content %}
<ul>
    {% for entry in entries %}
        <li>
            {% for category in entry.GamesGenre %}{{ category.title }}{% endfor %}: <a href="{{ entry.url }}">{{ entry.title }}</a> (***Put Title of Publisher Here***, {{ entry.GamesReleaseYear.format('Y') }})
        </li>
    {% endfor %}
</ul>
{% endblock %}

The place I want to insert the publisher's title is shown as: Put Title of Publisher Here

Thanks all. I just started using Craft about 3 hours ago for the first time!

1 Answer 1

1

This is untested but, give it a try:

{% extends "_layout" %}
{% set title = "Games" %}
{% set entries = craft.entries.section('games').limit(10).order('title') %}
{% block content %}
<ul>
    {% for entry in entries %}
        <li>
            {% for category in entry.GamesGenre %}{{ category.title }}{% endfor %}: <a href="{{ entry.url }}">{{ entry.title }}</a> {% set publishers = entry.GamesPublisher %}{% for publisher in publishers %}({{ publisher.title }}{% endfor %}, {{ entry.GamesReleaseYear.format('Y') }})
        </li>
    {% endfor %}
</ul>
{% endblock %}

If need be, have a look at An introduction to relations and reverse relations in Craft CMS, with examples

5
  • 3
    Congrats! You won the line length competition against @user1513427
    – carlcs
    Commented Dec 18, 2016 at 8:11
  • Yes, works like a charm. You saved my project!
    – DevServe
    Commented Dec 18, 2016 at 15:25
  • 1
    Not how I would have done it but, when in Rome.
    – Natetronn
    Commented Dec 18, 2016 at 16:59
  • @Natetronn, how would you have done it?
    – DevServe
    Commented Dec 19, 2016 at 14:33
  • Oh, don't listen to us. Do it how your team (or you) prefers it. That said, I might have added a bit more whitespace (spaces, tabs, newlines etc.) Check out to the docs to better understand what happens to your whitespace in Twig and how to control it, if need be.
    – Natetronn
    Commented Dec 20, 2016 at 19:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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