<p><code>craft.entries()</code> returns an <code>EntryQuery</code>. You must then <a href="https://craftcms.com/docs/3.x/element-queries.html#executing-element-queries" rel="nofollow noreferrer">execute the query</a> with one of those methods:</p>
<ul>
<li><code>one()</code></li>
<li><code>all()</code></li>
<li><code>count()</code></li>
<li><code>exists()</code></li>
</ul>
<p>In your case, I think <code>.exists()</code> is the best choice: <code>{% if categoryListings.exists() %}</code></p>
<p>Edit: You may also have to execute your category query with <code>{% for category in craft.categories().group('catGroup1').all() %}</code>. See <a href="https://craftcms.com/docs/3.x/categories.html#querying-categories" rel="nofollow noreferrer">https://craftcms.com/docs/3.x/categories.html#querying-categories</a>.</p>

CC BY-SA 4.0
2020-09-11T20:16:29.977Z
i tried your answers and hints, but no luck. maybe my approach with the craft.entries() is not the right one.
Henrik
CC BY-SA 4.0
2020-09-11T13:42:17.907Z
2020-09-11T14:01:27.040Z
2020-09-11T14:01:27.040Z
nstCactus
<p>I have entries, which can have several categories.
These categories are in different categorie groups.</p>
<p>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.</p>
<p>i tried this, but its not working, i think i get the relatedTo thing wrong:</p>
<pre><code>//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 %}
</code></pre>

CC BY-SA 4.0
2020-09-14T16:58:09.183Z
I'm not sure I understand how you set the entry type in the `kelims` section. Could you tell me more about your `filterThisCategory` field? A screenshot of the field settings would help. 
I'm also unsure I understood correctly what you try to achieve. You just want to output some HTML if the entry you're currently viewing has a specific category, right?
nstCactus
CC BY-SA 4.0
2020-09-19T20:30:52.460Z
My approach above was a wrong one. Regarding your last question, @nstCactus, yes, thats what i mean.
I eventually fount the solution, with the help of these pages:
https://craftcms.com/docs/3.x/categories-fields.html#templating,
https://craftcms.com/docs/3.x/categories.html#querying-categories

{% if entry.myFieldHandle.id('48, 49, 50, 51').exists() %}
 do this 
{% else %}
 do this
{% endif %}

myFieldHandle is the fieldHandle of the Category Group, where the categories i am searching for, are in.
Henrik
CC BY-SA 4.0
2020-09-19T20:35:56.793Z
@nstCactus, so your hint with the "exists()" was the right one, but i needed to add the ids.
Henrik
<filter><conditional><relatedto>
if entry has specific category, show specific html block
# if entry has specific category, show specific html block

(Asked by: username_0 on 2020-09-11)

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 %}

**Tags:** filter, conditional, relatedto

> **Comments:**
> **username_1**: I'm not sure I understand how you set the entry type in the `kelims` section. Could you tell me more about your `filterThisCategory` field? A screenshot of the field settings would help.  I'm also unsure I understood correctly what you try to achieve. You just want to output some HTML if the entry you're currently viewing has a specific category, right?  
> **username_0**: My approach above was a wrong one. Regarding your last question, @username_1, yes, thats what i mean. I eventually fount the solution, with the help of these pages: https://craftcms.com/docs/3.x/categories-fields.html#templating, https://craftcms.com/docs/3.x/categories.html#querying-categories  {% if entry.myFieldHandle.id('48, 49, 50, 51').exists() %}  do this  {% else %}  do this {% endif %}  myFieldHandle is the fieldHandle of the Category Group, where the categories i am searching for, are in.  
> **username_0**: @username_1, so your hint with the "exists()" was the right one, but i needed to add the ids.  

---

## 1 answers

### Answer by username_1 on 2020-09-11. Score: 1

`craft.entries()` returns an `EntryQuery`. You must then [execute the query](https://craftcms.com/docs/3.x/element-queries.html#executing-element-queries) 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>.

> **Comments:**
> **username_0**: i tried your answers and hints, but no luck. maybe my approach with the craft.entries() is not the right one.  

---

