<p>You might want the siblings of the selected Category via <code>siblingOf</code> - I think you'll need to do this with two steps.</p>

CC BY-SA 4.0
2019-08-17T00:21:23.070Z
2019-08-17T00:21:23.070Z
Judd Lyon
<p>Turns out my issue was that I was <strong>lazy-loading hotspotCategory</strong> which returns an array, making .level(1) impossible. Once removed from the lazyloading everything worked as expected.</p>

CC BY-SA 4.0
2019-08-17T10:59:50.323Z
2019-08-17T10:59:50.323Z
erwinheiser
<p>You have to get the first level and then you have to loop through them.</p>

<pre><code>{% set query = craft.categories().group('YOUR_CATEGORY').level(1) %}

{# Execute the query and get the results #}
{% set entries = query.all() %}

{% for entry in entries %}
   {{ entry.title }}
{% endif %}
</code></pre>

<p>This will output only the first level of the category tree.</p>

CC BY-SA 4.0
2019-08-17T10:40:46.843Z
2019-08-17T10:40:46.843Z
Christopher Dosin
<p>Having a bit of a brainfart here...
If hotspotCategory is my entry's categoryfield and that category group has nested categories, how do I return only the top level categories?</p>

<pre><code>{% set entryCats = entry.hotspotCategory %}
{% if entryCats | length %}
    &lt;span class="filter_cat"&gt;
       {% for cat in entryCats %}
          {{ cat.title }} {% if not loop.last %}, {% endif %}
       {% endfor %}
     &lt;/span&gt;
 {% endif %} 
</code></pre>

<p>this works but if I do something like</p>

<pre><code>{% set entryCats = entry.hotspotCategory.level(1).all() %}
</code></pre>

<p>I get an</p>

<pre><code>Impossible to invoke a method ("level") on an array.
</code></pre>

<p>error.</p>

<p>What am I missing here?</p>

<p><strong>To clarify</strong>: my entry has the following categories attached (slow &amp; fast food are subcategories):</p>

<pre><code>- food &amp; dining
--- slow food
--- fast food
</code></pre>

<p>Now, on the entry page I only want to display the top level category, not the subcategories.</p>

<craft3><categories><category>
Show only top level categories assigned to an entry
# Show only top level categories assigned to an entry

(Asked by: username_0 on 2019-08-16)

Having a bit of a brainfart here...
If hotspotCategory is my entry's categoryfield and that category group has nested categories, how do I return only the top level categories?
{% set entryCats = entry.hotspotCategory %}
{% if entryCats | length %}
    
       {% for cat in entryCats %}
          {{ cat.title }} {% if not loop.last %}, {% endif %}
       {% endfor %}
     
 {% endif %} 

this works but if I do something like
{% set entryCats = entry.hotspotCategory.level(1).all() %}

I get an
Impossible to invoke a method ("level") on an array.

error.
What am I missing here?
To clarify: my entry has the following categories attached (slow & fast food are subcategories):
- food & dining
--- slow food
--- fast food

Now, on the entry page I only want to display the top level category, not the subcategories.

**Tags:** craft3, categories, category


---

## 3 answers

### Answer by username_1 on 2019-08-17. Score: 1

You might want the siblings of the selected Category via `siblingOf` - I think you'll need to do this with two steps.


---

### Answer by username_0 on 2019-08-17. Score: 1

Turns out my issue was that I was **lazy-loading hotspotCategory** which returns an array, making .level(1) impossible. Once removed from the lazyloading everything worked as expected.


---

### Answer by username_2 on 2019-08-17. Score: 1

You have to get the first level and then you have to loop through them.
{% set query = craft.categories().group('YOUR_CATEGORY').level(1) %}

{# Execute the query and get the results #}
{% set entries = query.all() %}

{% for entry in entries %}
   {{ entry.title }}
{% endif %}

This will output only the first level of the category tree.


---

