<p>Instead of using <em>nav</em>, you can just use your own code for getting children. I.e. loop through level 1, then within that loop check if they have ancestors at level 2, then within that loop check if they have ancestors for level 3 etc.</p>

CC BY-SA 4.0
2019-09-02T15:26:26.463Z
2019-09-02T15:26:26.463Z
dmatthams
<p>The following is using commerce product entries, but I'm reasonably sure it could be any entry.</p>

<p>With the following category taxonomy:</p>

<pre><code>- Category 1
    - Category 1A
- Category 2
- Category 3
...
</code></pre>

<p>If I have a product entry "Product 1" with a category field selected to "Category 1A", when looping though 
all categories to return products for that category, how can I list the entry ONLY for "Category 1A" and not it's any of its ancestors e.g. "Category 1"?</p>

<p>Currently, I'm using the following:</p>

<pre><code>{% nav category in productCategories %}
    &lt;li&gt;
        {{ category.title }}
        {% set products = craft.products().relatedTo(category).all() %}
        {% for product in products %}
            {{ product.title }}
        {% endfor %}
        {% ifchildren %}
            &lt;ul&gt;
                {% children %}
            &lt;/ul&gt;
        {% endifchildren %}
    &lt;/li&gt;
{% endnav %}
</code></pre>

<p>Which returns</p>

<pre><code>- Category 1
    Product 1
    - Category 1A
        Product 1
- Category 2
- Category 3
...
</code></pre>

<p>But what I want is for it to only be visible on the selected category.</p>

<pre><code>- Category 1
    - Category 1A
        Product 1
- Category 2
- Category 3
...
</code></pre>

<p>Is there a way to do this using categories? or am I best using a structure?</p>

CC BY-SA 4.0
2019-09-02T11:22:55.003Z
Using a structure seems to work however I would like to retain the ability of getting all entries for a parent category, best of both worlds!?
Matt Jenkins
<templating><categories><entry><relatedto>
Get entries related to selected category only and not it's ancestors
# Get entries related to selected category only and not it's ancestors

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

The following is using commerce product entries, but I'm reasonably sure it could be any entry.
With the following category taxonomy:
- Category 1
    - Category 1A
- Category 2
- Category 3
...

If I have a product entry "Product 1" with a category field selected to "Category 1A", when looping though 
all categories to return products for that category, how can I list the entry ONLY for "Category 1A" and not it's any of its ancestors e.g. "Category 1"?
Currently, I'm using the following:
{% nav category in productCategories %}
    
        {{ category.title }}
        {% set products = craft.products().relatedTo(category).all() %}
        {% for product in products %}
            {{ product.title }}
        {% endfor %}
        {% ifchildren %}
            
                {% children %}
            
        {% endifchildren %}
    
{% endnav %}

Which returns
- Category 1
    Product 1
    - Category 1A
        Product 1
- Category 2
- Category 3
...

But what I want is for it to only be visible on the selected category.
- Category 1
    - Category 1A
        Product 1
- Category 2
- Category 3
...

Is there a way to do this using categories? or am I best using a structure?

**Tags:** templating, categories, entry, relatedto
> **Comments:**
> **username_0**: Using a structure seems to work however I would like to retain the ability of getting all entries for a parent category, best of both worlds!?  

---

## 1 answers

### Answer by username_1 on 2019-09-02. Score: 1

Instead of using *nav*, you can just use your own code for getting children. I.e. loop through level 1, then within that loop check if they have ancestors at level 2, then within that loop check if they have ancestors for level 3 etc.


---

