<p>This won't be super efficient if you have a loads of teams/people as it will run an <code>entry</code> query per subteam (and it won't check for empty teams either) but it should get you started.</p>
<pre><code>{% set categories = craft
    .categories()
    .group('team')
    .level(1)
    .with('children')
    .all() %}

{% for category in categories %}
&lt;div class=&quot;container mx-auto bg-red-300 flex&quot;&gt;
    &lt;p class=&quot;flex-none w-full text-6xl my-20&quot;&gt;&lt;a href=&quot;{{ category.url }}&quot;&gt;{{ category.title }}&lt;/a&gt;&lt;/p&gt;
    {% for child in category.children %}
        &lt;div class=&quot;bg-green-700 text-3xl&quot;&gt;
            {{ child.title }}
        &lt;/div&gt;
        {% for entry in craft.entries.section('team').relatedTo(child).all() %}
            &lt;div class=&quot;bg-green-500&quot;&gt;
                &lt;dl&gt;
                    &lt;dt style=&quot;color:#EB8B2D;&quot;&gt;{{entry.title}}&lt;/dt&gt;

                    &lt;dd&gt;{{entry.positionTitle}}
                    &lt;/dd&gt;
                    &lt;dd&gt;&lt;a href=&quot;mailto:{{entry.emailAddress}}&quot;&gt;{{entry.emailAddress}}&lt;/a&gt;&lt;/dd&gt;
                    &lt;dd&gt;&lt;strong&gt;Tel: &lt;/strong&gt;{{entry.telephoneNumber}}&lt;/dd&gt;
                    &lt;dd&gt;&lt;strong&gt;Mob:&lt;/strong&gt; {{entry.mobileNumber}}&lt;/dd&gt;
                &lt;/dl&gt;
            &lt;/div&gt;
        {% endfor %}
    {% endfor %}
&lt;/div&gt;
{% endfor %}
</code></pre>

CC BY-SA 4.0
2020-07-24T14:25:02.260Z
2020-07-24T14:25:02.260Z
Oli
<p>I'm struggling to loop through level 1 categories
and display as a category title
and then show nested level 2 categories (via a category title) under which I want to nest related categories
to achieve:</p>
<p>PARENT CATEGORY</p>
<ul>
<li>CHILD CATEGORY</li>
<li>
<ul>
<li>related entry</li>
</ul>
</li>
<li>
<ul>
<li>related entry</li>
</ul>
</li>
<li>
<ul>
<li>related entry</li>
</ul>
</li>
<li>CHILD CATEGORY</li>
<li>
<ul>
<li>related entry</li>
</ul>
</li>
<li>
<ul>
<li>related entry</li>
</ul>
</li>
<li>
<ul>
<li>related entry</li>
</ul>
</li>
</ul>
<p>although the content and order is correct, I have divs closing in the wrong place so can't control which content is positioned by flexbox using this code:</p>
<pre><code>{% for category in craft.categories.group('team') %}

{% if category.level == 1 %}

&lt;div class=&quot;container mx-auto bg-red-300 flex&quot;&gt;    

     &lt;p class=&quot;flex-none w-full text-6xl my-20&quot;&gt;&lt;a href=&quot;{{ category.url }}&quot;&gt;{{ category.title }} LEVEL 1&lt;/a&gt;&lt;/p&gt;    

     {% endif %}

     &lt;div class=&quot;container mx-auto bg-green-300  flex&quot;&gt;

          &lt;div class=&quot;w-1/4&quot;&gt;
               {% if category.level == 2 %}
               &lt;div class=&quot;bg-green-700 text-3xl&quot;&gt;
               {{ category.title }} LEVEL 2
               &lt;/div&gt;
               {% for entry in craft.entries.section('team').relatedTo(category).all() %}       
 &lt;div class=&quot;bg-green-500&quot;&gt;
               &lt;dl&gt;
                    &lt;dt style=&quot;color:#EB8B2D;&quot;&gt;{{entry.title}}&lt;/dt&gt;

                    &lt;dd&gt;{{entry.positionTitle}}
                    &lt;/dd&gt;
                    &lt;dd&gt;&lt;a href=&quot;mailto:{{entry.emailAddress}}&quot;&gt;{{entry.emailAddress}}&lt;/a&gt;&lt;/dd&gt;
                    &lt;dd&gt;&lt;strong&gt;Tel: &lt;/strong&gt;{{entry.telephoneNumber}}&lt;/dd&gt;
                    &lt;dd&gt;&lt;strong&gt;Mob:&lt;/strong&gt; {{entry.mobileNumber}}&lt;/dd&gt;
               &lt;/dl&gt;
&lt;/div&gt; 
               {% endfor %} 
               
               {% endif %}
          &lt;/div&gt;
     &lt;/div&gt;

&lt;/div&gt;

{% endfor %}
</code></pre>

CC BY-SA 4.0
2020-07-24T07:48:15.253Z
Splitting opening/closing html tags in different conditionals is definitely something to be avoided at all costs, it's nasty... With that in mind, it's not clear what you want your end markup to look like, but it's likely that the `group` filter will be needed. (It's easy enough when you have a single level of categories but will get complex with multiple levels)... depends on what the final markup should be like... can you post an ideal final result? https://docs.craftcms.com/v3/dev/filters.html#group
James Smith
<categories><nested>
Display parent category with nested child categories and related entries
# Display parent category with nested child categories and related entries

(Asked by: username_0 on 2020-07-23)

I'm struggling to loop through level 1 categories
and display as a category title
and then show nested level 2 categories (via a category title) under which I want to nest related categories
to achieve:
PARENT CATEGORY

CHILD CATEGORY


related entry




related entry




related entry


CHILD CATEGORY


related entry




related entry




related entry



although the content and order is correct, I have divs closing in the wrong place so can't control which content is positioned by flexbox using this code:
{% for category in craft.categories.group('team') %}

{% if category.level == 1 %}

    

     {{ category.title }} LEVEL 1    

     {% endif %}

     

          
               {% if category.level == 2 %}
               
               {{ category.title }} LEVEL 2
               
               {% for entry in craft.entries.section('team').relatedTo(category).all() %}       
 
               
                    {{entry.title}}

                    {{entry.positionTitle}}
                    
                    {{entry.emailAddress}}
                    Tel: {{entry.telephoneNumber}}
                    Mob: {{entry.mobileNumber}}
               
 
               {% endfor %} 
               
               {% endif %}
          
     



{% endfor %}

**Tags:** categories, nested

> **Comments:**
> **username_1**: Splitting opening/closing html tags in different conditionals is definitely something to be avoided at all costs, it's nasty... With that in mind, it's not clear what you want your end markup to look like, but it's likely that the `group` filter will be needed. (It's easy enough when you have a single level of categories but will get complex with multiple levels)... depends on what the final markup should be like... can you post an ideal final result? https://docs.craftcms.com/v3/dev/filters.html#group  

---

## 1 answers

### Answer by username_2 on 2020-07-24. Score: 1 | Accepted answer

This won't be super efficient if you have a loads of teams/people as it will run an entry query per subteam (and it won't check for empty teams either) but it should get you started.
{% set categories = craft
    .categories()
    .group('team')
    .level(1)
    .with('children')
    .all() %}

{% for category in categories %}

    {{ category.title }}
    {% for child in category.children %}
        
            {{ child.title }}
        
        {% for entry in craft.entries.section('team').relatedTo(child).all() %}
            
                
                    {{entry.title}}

                    {{entry.positionTitle}}
                    
                    {{entry.emailAddress}}
                    Tel: {{entry.telephoneNumber}}
                    Mob: {{entry.mobileNumber}}
                
            
        {% endfor %}
    {% endfor %}

{% endfor %}


---

