2

Trying to list all the entries which belong to an entry. Currently I have the following field format:

  • Product
    • Has an 'retailer' entries field.
  • Retailer
    • Normal entry.

On the retailers _entry.html page I'm trying to display all the products which belong to said retailer. But the code I'm producing is running into trouble:

retailers/_entry.html

    {% set relatedProducts = craft.entries.section('products').relatedTo(entry) %}

    {% for entry in relatedProducts %}
        <h3>Some great entries</h3>
    {% endfor %}

This code seems to be showing the title but not actually looping over the length of the array?

3

I had a similar problem to yours, and here's what I ran into.

It may be that you're using entry for your main info on the retailer entry page? Because {{ for entry in relatedProducts }} would return one result (the retailer entry). So instead, use a different variable for the loop.

Try something more along the lines of this:

<h1>{{ entry.title }}</h1>
<div>{{ entry.body }}</div>

{% if craft.entries.section('products').relatedTo(entry)|length %}
  {% set relatedProducts = craft.entries.section('products').relatedTo(entry) %}
  <h3>Some great entries</h3>

  {% for relatedEntry in relatedProducts %}
   {{ relatedEntry.title }}
   {{ relatedEntry.body }}
  {% endfor %}
{% endif %}

Also, note that I wrapped the whole thing in an if-check. It checks to see if there are related products first, and then, if there are, sets the variable and runs the loop.

And your H3 should be outside the loop, otherwise it'll show up on every pass around.

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.