<p>The final solution I got to work:</p>

<pre><code>{% set cats = entry.categories %}
{% for cat in cats %}
    {% if cat.slug == 'secondlevel1' or cat.slug == 'secondlevel2' %}
        {% set catChildren = cats.descendantOf(cat.id).descendantDist('1') %}
        {% if catChildren | length %}
            {% for childCat in catChildren %}
                {% set image = childCat.buildingBlockIcon.first() %}
                    {% if image %}
                        &lt;img alt="{{ childCat.title }}" src="{{ image.url }}" /&gt;
                    {% endif %}
            {% endfor %}
        {% endif %}
    {% endif %}
{% endfor %}
</code></pre>

CC BY-SA 3.0
2015-11-25T16:47:19.723Z
2015-11-25T16:47:19.723Z
Lettie
<p>I have a entry field populated by categories. The number of categories an entry can have is unlimited and the categories are set up as nested categories i.e. </p>

<ul>
<li>first level</li>
<li>---second level</li>
<li><p>------third level</p></li>
<li><p>first level 2</p></li>
<li>---second level 2</li>
<li>------third level 2</li>
</ul>

<p>I want to be able to list out any categories selected at the third level that the entry may have selected if a certain parent category is identified. So far I have the following code. This works but lists all child categories for the identified parent instead of just the selected ones.</p>

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

<pre><code>{% set cats = entry.categories %}
{% for cat in cats %}
    {% if cat.slug == 'secondlevel1' or cat.slug == 'secondlevel2' %}
        {% set catChildren = cats.descendantOf(cat.id).descendantDist('1') %}
        {% if catChildren | length %}
            {% for childCat in catChildren %}
                {% set image = childCat.buildingBlockIcon.first() %}
                    {% if image %}
                        &lt;img alt="{{ childCat.title }}" src="{{ image.url }}" /&gt;
                    {% endif %}
            {% endfor %}
        {% endif %}
    {% endif %}
{% endfor %}
</code></pre>

<p>----- EDIT Solution -----</p>

<p>I worked this out</p>

<p>by changing one line of code line 4 above from</p>

<pre><code>{% set catChildren = cat.level(2).children %}
</code></pre>

<p>to</p>

<pre><code>{% set catChildren = cats.descendantOf(cat.id).descendantDist('1') %}
</code></pre>

<p>Now works a treat. Any better solutions feel free to post.</p>

CC BY-SA 3.0
2015-11-24T20:42:54.030Z
Give [this thread](http://craftcms.stackexchange.com/questions/3058/display-entries-related-to-a-category-only-bottom-level) a peek. I think its very similar to what you are looking for.
Damon
CC BY-SA 3.0
2015-11-25T10:52:56.010Z
@damon thanks for the link. But it's kind of the opposite to what I want to achieve. I'm looking at in reverse. I have the entry, from the entry I want to  obtain what third level categories have been assigned to it and then use these to link to a related entries page. At present I can only get it to list all third level categories and not just the ones that were selected for the specific entry. So for example if the third level has 4 items and the entry only has 2 selected it still outputs all 4 items instead of just the selected 2. So I need a way to identify which items were selected.
Lettie
CC BY-SA 3.0
2015-11-25T11:32:27.057Z
@damon I've managed to solve it by using the descendantOf instead of level. I've edited my post with the answers above. Thanks for the clues!
Lettie
CC BY-SA 3.0
2015-11-25T16:13:56.227Z
@Lettie would you mind adding the solution you found as an official answer instead of an edit to the original question?
Brad Bell
CC BY-SA 3.0
2015-11-25T16:46:20.880Z
@bradbell no problem
Lettie
<entries><categories><children>
How to list an entries selected nested categories?
# How to list an entries selected nested categories?

(Asked by: username_0 on 2015-11-24)

I have a entry field populated by categories. The number of categories an entry can have is unlimited and the categories are set up as nested categories i.e. 

first level
---second level
------third level
first level 2
---second level 2
------third level 2

I want to be able to list out any categories selected at the third level that the entry may have selected if a certain parent category is identified. So far I have the following code. This works but lists all child categories for the identified parent instead of just the selected ones.
What am I missing?
{% set cats = entry.categories %}
{% for cat in cats %}
    {% if cat.slug == 'secondlevel1' or cat.slug == 'secondlevel2' %}
        {% set catChildren = cats.descendantOf(cat.id).descendantDist('1') %}
        {% if catChildren | length %}
            {% for childCat in catChildren %}
                {% set image = childCat.buildingBlockIcon.first() %}
                    {% if image %}
                        
                    {% endif %}
            {% endfor %}
        {% endif %}
    {% endif %}
{% endfor %}

----- EDIT Solution -----
I worked this out
by changing one line of code line 4 above from
{% set catChildren = cat.level(2).children %}

to
{% set catChildren = cats.descendantOf(cat.id).descendantDist('1') %}

Now works a treat. Any better solutions feel free to post.

**Tags:** entries, categories, children

> **Comments:**
> **username_1**: Give [this thread](http://craftcms.stackexchange.com/questions/3058/display-entries-related-to-a-category-only-bottom-level) a peek. I think its very similar to what you are looking for.  
> **username_0**: @damon thanks for the link. But it's kind of the opposite to what I want to achieve. I'm looking at in reverse. I have the entry, from the entry I want to  obtain what third level categories have been assigned to it and then use these to link to a related entries page. At present I can only get it to list all third level categories and not just the ones that were selected for the specific entry. So for example if the third level has 4 items and the entry only has 2 selected it still outputs all 4 items instead of just the selected 2. So I need a way to identify which items were selected.  
> **username_0**: @damon I've managed to solve it by using the descendantOf instead of level. I've edited my post with the answers above. Thanks for the clues!  
> **username_2**: @username_0 would you mind adding the solution you found as an official answer instead of an edit to the original question?  
> **username_0**: @bradbell no problem  

---

## 1 answers

### Answer by username_0 on 2015-11-25. Score: 1

The final solution I got to work:
{% set cats = entry.categories %}
{% for cat in cats %}
    {% if cat.slug == 'secondlevel1' or cat.slug == 'secondlevel2' %}
        {% set catChildren = cats.descendantOf(cat.id).descendantDist('1') %}
        {% if catChildren | length %}
            {% for childCat in catChildren %}
                {% set image = childCat.buildingBlockIcon.first() %}
                    {% if image %}
                        
                    {% endif %}
            {% endfor %}
        {% endif %}
    {% endif %}
{% endfor %}


---

