1

I have a channel (handle: "infopages") with entries. I have a channel (handle: "products") with entries. I have a category (handle: "productTypes")

Every entry from "infopages" has a category field, called "relatedCat". Every entry from "products" has a category field, called "relatedCat".

In the template for the infopages, I want to show all entries from "products" that share the same category. So if an infopage is related to the category "robotmowers", I want to show all the entries from "products" that are also related to "robotmowers". Every entry in every channel can be related to multiple categories, that's where the hard part comes in.

I could loop through the "relatedCat" field and get all the related entries from "products" in a loop. However, if multiple categories are selected, that means I possible get the same product entry more than once. I guess there must be a better way to do this?

1 Answer 1

2

If you have infoPage with category A and B lets say.

Then, you have a Product 1 with category A and B also.

If in your template you do (with entry being the current infoPage entry):

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

{% for relatedProduct in relatedProducts %}
    {{ relatedProduct.title }} <br>
{% endfor %}

You should not get Product 1 returned to you twice..? Have you actually tried?

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.