<pre><code>{# Here, we grab segment of a URI's to use in conditional statements for displaying category levels. Conditionals aren't here. Will have to do it by yourself. #}
{% set firstSeg = craft.request.getSegment(1) %}
{% set secondSeg = craft.request.getSegment(2) %}
{% set thirdSeg = craft.request.getSegment(3) %}
{% set fourthSeg = craft.request.getSegment(4) %}

{# Grab all of the entry ids from the section, which we want to display categories for. #}
{% set entryIds = craft.entries.section('products').ids() %}


{# Here we grab the category group and a certain level, which we want to display categories for, and relate them to 'entryIds' #}
{% set firstLvls = craft.categories.relatedTo({ sourceElement: entryIds }).group('categories').level(1) %}
{% set secondLvls = craft.categories.relatedTo({ sourceElement: entryIds }).group('categories').level(2) %}
{% set thirdLvls = craft.categories.relatedTo({ sourceElement: entryIds }).group('categories').level(3) %}
{% set fourthLvls = craft.categories.relatedTo({ sourceElement: entryIds }).group('categories').level(4) %}

{# We loop through first level of categories. Constructively, it's the navigation menu located at 'site.com/' #}
{% for firstLvl in firstLvls %}
    &lt;li&gt;
        &lt;a href="{{ firstLvl.getUrl }}"&gt;{{ firstLvl.title|replace({'-':' '}) }}&lt;/a&gt;
    &lt;/li&gt;
{% endfor %}﻿

{# We loop through second level of categories. Constructively, it's the first category's selection menu
located at 'site.com/catLevel1' #}
{% for secondLvl in secondLvls %}
    &lt;li&gt;
        &lt;a href="{{ secondLvl.getUrl }}"&gt;{{ secondLvl.title|replace({'-':' '}) }}&lt;/a&gt;
    &lt;/li&gt;
{% endfor %}﻿

{# We loop through third level of categories. Constructively, it's the second category's selection menu
located at 'site.com/catLevel1/catLevel2' #}
{% for thirdLvl in thirdLvls %}
    &lt;li&gt;
        &lt;a href="{{ thirdLvl.getUrl }}"&gt;{{ thirdLvl.title|replace({'-':' '}) }}&lt;/a&gt;
    &lt;/li&gt;
{% endfor %}﻿

{# We loop through fourth level of categories. Constructively, it's the third category's selection menu
located at 'site.com/catLevel1/catLevel2/catLevel3' #}
{% for fourthLvl in fourthLvls %}
    &lt;li&gt;
        &lt;a href="{{ fourthLvl.getUrl }}"&gt;{{ fourthLvl.title|replace({'-':' '}) }}&lt;/a&gt;
    &lt;/li&gt;
{% endfor %}﻿
</code></pre>

CC BY-SA 3.0
2015-10-25T08:49:38.220Z
2015-10-25T08:49:38.220Z
Dominik Krulak
<p>If I understand your issue correctly, try changing </p>

<pre><code>{% set topLevelCats = craft.categories.relatedTo({ sourceElement: entryIds }).group('filter').level(1) %}
{% set secondLevelCats = topLevelCats.slug(secondSeg) %}
</code></pre>

<p>to</p>

<pre><code>{% set topLevelCats = craft.categories.relatedTo({ sourceElement: entryIds }).group('filter').level(1) %}
{% set secondLevelCats = craft.categories.slug(secondSeg).level(2) %}
</code></pre>

CC BY-SA 3.0
2015-10-20T14:20:20.253Z
I've tried this way as first thing, but I can report here, that it gave me empty nothing. Digging deeper!
Dominik Krulak
CC BY-SA 3.0
2015-10-20T15:25:02.513Z
Do you get any categories returned if you just do `{% set secondLevelCats = craft.categories.level(2) %}`?
Aaron Berkowitz
CC BY-SA 3.0
2015-10-20T15:39:54.890Z
Yes, I get all sub-categories of level 1.
Dominik Krulak
CC BY-SA 3.0
2015-10-25T08:41:10.920Z
I found simpler way to do it, but the problem we were having was solved by `{% set secondLevelCats = craft.categories.relatedTo({ sourceElement: entryIds }).group('filter').level(2) %}`
Dominik Krulak
CC BY-SA 3.0
2015-10-20T13:11:56.770Z
2015-10-20T13:11:56.770Z
Aaron Berkowitz
<p>I try to explain you semantically what I want to achieve here. I couldn't make it shorter. Sorry.</p>

<ol>
<li>Navigation will be constructed from <code>.level(1)</code> categories.</li>
<li><p>At "site.com/firstLevelCat" I want to display list of populated <code>.level(2)</code> categories like this</p>

<ul>
<li>site.com/firstLevelCat/secondLevelCat</li>
<li>site.com/firstLevelCat/secondLevelCat2</li>
<li>site.com/firstLevelCat/secondLevelCat3</li>
</ul></li>
<li><p>At "site.com/firstLevelCat/secondLevelCat" I'd like to display list of <code>.level(2)</code> categories along with a list of <code>.level(3)</code> categories like this</p>

<ul>
<li>site.com/firstLevelCat/secondLevelCat/thirdLevelCat</li>
<li>site.com/firstLevelCat/secondLevelCat2/thirdLevelCat</li>
<li>site.com/firstLevelCat/secondLevelCat3/thirdLevelCat</li>
</ul></li>
</ol>

<p>I intend to build it up to 4th level.</p>

CC BY-SA 3.0
2015-10-20T00:19:31.223Z
Hi Dominik - do you have [devMode](http://buildwithcraft.com/docs/config-settings#devMode) enabled to show any obvious errors?
Aaron Berkowitz
CC BY-SA 3.0
2015-10-20T00:28:16.387Z
Also, what are you trying to do here: `{% set secondLevelCats = topLevelCats.slug(secondSeg) %}`? Are you trying to get a single category or all second level categories that are children of `topLevelCats`?
Aaron Berkowitz
CC BY-SA 3.0
2015-10-20T06:37:51.540Z
Hi, Yes I always do have devMode enabled and in current situation, there aren't any errors.
Dominik Krulak
CC BY-SA 3.0
2015-10-20T06:45:27.667Z
To your second question @Aaron Berkowitz, I'm trying to display sub-categories of the selected top level category, using second segment from URI. Theoretically, I could use this method up to a level category I need.
Dominik Krulak
<categories>
How do I only display list of categories at certain level of parent category that have entries
# How do I only display list of categories at certain level of parent category that have entries

(Asked by: username_0 on 2015-10-19)

I try to explain you semantically what I want to achieve here. I couldn't make it shorter. Sorry.

1. Navigation will be constructed from `.level(1)` categories.
2. At "site.com/firstLevelCat" I want to display list of populated `.level(2)` categories like this

   * site.com/firstLevelCat/secondLevelCat
   * site.com/firstLevelCat/secondLevelCat2
   * site.com/firstLevelCat/secondLevelCat3
3. At "site.com/firstLevelCat/secondLevelCat" I'd like to display list of `.level(2)` categories along with a list of `.level(3)` categories like this

   * site.com/firstLevelCat/secondLevelCat/thirdLevelCat
   * site.com/firstLevelCat/secondLevelCat2/thirdLevelCat
   * site.com/firstLevelCat/secondLevelCat3/thirdLevelCat

I intend to build it up to 4th level.

**Tags:** categories

> **Comments:**
> **username_1**: Hi Dominik - do you have [devMode](http://buildwithcraft.com/docs/config-settings#devMode) enabled to show any obvious errors?  
> **username_1**: Also, what are you trying to do here: `{% set secondLevelCats = topLevelCats.slug(secondSeg) %}`? Are you trying to get a single category or all second level categories that are children of `topLevelCats`?  
> **username_0**: Hi, Yes I always do have devMode enabled and in current situation, there aren't any errors.  
> **username_0**: To your second question @username_1, I'm trying to display sub-categories of the selected top level category, using second segment from URI. Theoretically, I could use this method up to a level category I need.  

---

## 2 answers

### Answer by username_0 on 2015-10-25. Score: 1 | Accepted answer

{# Here, we grab segment of a URI's to use in conditional statements for displaying category levels. Conditionals aren't here. Will have to do it by yourself. #}
{% set firstSeg = craft.request.getSegment(1) %}
{% set secondSeg = craft.request.getSegment(2) %}
{% set thirdSeg = craft.request.getSegment(3) %}
{% set fourthSeg = craft.request.getSegment(4) %}

{# Grab all of the entry ids from the section, which we want to display categories for. #}
{% set entryIds = craft.entries.section('products').ids() %}


{# Here we grab the category group and a certain level, which we want to display categories for, and relate them to 'entryIds' #}
{% set firstLvls = craft.categories.relatedTo({ sourceElement: entryIds }).group('categories').level(1) %}
{% set secondLvls = craft.categories.relatedTo({ sourceElement: entryIds }).group('categories').level(2) %}
{% set thirdLvls = craft.categories.relatedTo({ sourceElement: entryIds }).group('categories').level(3) %}
{% set fourthLvls = craft.categories.relatedTo({ sourceElement: entryIds }).group('categories').level(4) %}

{# We loop through first level of categories. Constructively, it's the navigation menu located at 'site.com/' #}
{% for firstLvl in firstLvls %}
    
        {{ firstLvl.title|replace({'-':' '}) }}
    
{% endfor %}﻿

{# We loop through second level of categories. Constructively, it's the first category's selection menu
located at 'site.com/catLevel1' #}
{% for secondLvl in secondLvls %}
    
        {{ secondLvl.title|replace({'-':' '}) }}
    
{% endfor %}﻿

{# We loop through third level of categories. Constructively, it's the second category's selection menu
located at 'site.com/catLevel1/catLevel2' #}
{% for thirdLvl in thirdLvls %}
    
        {{ thirdLvl.title|replace({'-':' '}) }}
    
{% endfor %}﻿

{# We loop through fourth level of categories. Constructively, it's the third category's selection menu
located at 'site.com/catLevel1/catLevel2/catLevel3' #}
{% for fourthLvl in fourthLvls %}
    
        {{ fourthLvl.title|replace({'-':' '}) }}
    
{% endfor %}﻿


---

### Answer by username_1 on 2015-10-20. Score: 1

If I understand your issue correctly, try changing 
{% set topLevelCats = craft.categories.relatedTo({ sourceElement: entryIds }).group('filter').level(1) %}
{% set secondLevelCats = topLevelCats.slug(secondSeg) %}

to
{% set topLevelCats = craft.categories.relatedTo({ sourceElement: entryIds }).group('filter').level(1) %}
{% set secondLevelCats = craft.categories.slug(secondSeg).level(2) %}

> **Comments:**
> **username_0**: I've tried this way as first thing, but I can report here, that it gave me empty nothing. Digging deeper!  
> **username_1**: Do you get any categories returned if you just do `{% set secondLevelCats = craft.categories.level(2) %}`?  
> **username_0**: Yes, I get all sub-categories of level 1.  
> **username_0**: I found simpler way to do it, but the problem we were having was solved by `{% set secondLevelCats = craft.categories.relatedTo({ sourceElement: entryIds }).group('filter').level(2) %}`  

---

