Craft CMS Asked by Henrik on April 22, 2021
I have entries, which can have several categories.
These categories are in different categorie groups.
Now if i show an entry page, i want to display i certain html block
based on a specific category, the entry has or not.
i tried this, but its not working, i think i get the relatedTo thing wrong:
//i loop through all the categories in my group
{% for category in craft.categories.group('catGroup1') %}
//the i go to the section my entries are in and relate that to the categories i want to filter
{% set categoryListings = craft.entries({
section: 'kelims',
relatedTo: [
{ targetElement: category, field: 'filterThisCategory' }
]
}) %}
//if the entry has this category
{% if categoryListings|length %}
display some html block
{% endif %}
{% endfor %}
craft.entries()
returns an EntryQuery
. You must then execute the query with one of those methods:
one()
all()
count()
exists()
In your case, I think .exists()
is the best choice: {% if categoryListings.exists() %}
Edit: You may also have to execute your category query with {% for category in craft.categories().group('catGroup1').all() %}
. See https://craftcms.com/docs/3.x/categories.html#querying-categories.
Answered by nstCactus on April 22, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP