0

Doing my first Craft job and stuck with a reverse related entries thing. I’ve got a channel of “Sectors” and a channel of “News”. The entries in news have a relationship field back to the “Sectors”. When I’m viewing a “Sector” entry, I want to show links to entries in News that have a relationship to it.

I’ve tried a few things but essentially I’ve got this…

{% extends "_layout" %}

{% block content %}
    <div class="panel">
    <h1>{{ entry.title }}</h1>

    {{ entry.body }}
    </div>

    {% for entry in craft.entries.section('news').relatedTo(entry) %}
        {{ entry.title }}
    {% endfor %}
{% endblock %}

But, even though I have a news entry related to a specific Sector entry, the above code doesn't show any related entries.

Oddly, if I do this...

{% for entry in craft.entries.section('news').relatedTo(entry) %}

Then I get some output but it shows me something from a different channel.

I’m confused. Can someone help?

Update

Schoolboy error alert!

I had the wrong field in my news entries. There was a category field set up which had the same values as the sector names. Once I'd replaced the field with the proper one it all worked as expected!

  • You are reassigning the value of the entry variable in your for loop. What happens when you change the for loop to: {% for relatedEntry in craft.entries.section('news').relatedTo(entry) %}. Then output {{ relatedEntry.title }}. – Steve Rowling Jul 2 '15 at 15:12
  • I thought that was the problem (and it may have been an issue). But I found the real problem. Stupid error on my part. I'll update my question to explain. – foamcow Jul 2 '15 at 15:32
  • 1
    Glad you got it sorted, @foamcow. Can you add your update as an official answer? – Brad Bell Jul 2 '15 at 16:49
1

Schoolboy error alert!

I had the wrong field in my news entries. There was a category field set up which had the same values as the sector names. Once I'd replaced the field with the proper one it all worked as expected!

|improve this answer|||||

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.