0

Ok all - this is bound to be ridiculously simple, as it's one of the most basic things you can do (https://craftcms.com/support/entries-related-to-category) - but I am ill and I can not figure out what I've done wrong.

I have a category group 'Things To Do'. That group is set to allow categories in it to have their own URLs. The URL format is: things-to-do/{slug} and it uses the template 'things-to-do/category'.

I also have a channel Things To Do, the URL format being 'things-to-do/{slug}' and using a different template 'things-to-do'.

If I go to the url for a category that exists, it loads the correct template and I can output the correct fields attached to the category itself; e.g., {{ category.title }} or {{ category.myCustomField }}

However, trying to output entries related to the category I'm viewing is instead just listing all entries regardless of their lack of association with the category...

{% set thingsToDo = craft.entries.relatedTo(category) %}
{% for thingToDo in thingsToDo %}
    ...
{% endfor %}

This should only be listing one entry for a given category, but it's listing all entries.

What have a screwed up?

2
  • Does it help if you're explicit about which section the entries should be from? Jan 13, 2016 at 17:56
  • Nope, makes no difference, and the entries are in the same section anyway. This seems really odd. Jan 14, 2016 at 9:03

1 Answer 1

2

Mystery solved; the relation code was too general; some of the entries were related to the category on the homepage, and the example code doesn't care about how or where the entries are related to the category as long as they are somewhere somehow.

I needed to be more specific:

{% set thingsToDo = craft.entries.relatedTo({
    targetElement: category,
    field: 'typeOfThing'
}) %}

Works.

Your Answer

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

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