<p>Check out the documentation for <a href="http://buildwithcraft.com/docs/templating/craft.categories">craft.categories</a> which should do what you need. In this example, I assume that 'landing pages' are the same as your level 1 categories, in which case you don't need to deal with the entries at all:</p>

<pre><code>{% set categories = craft.categories.group('navCategories') %}

&lt;ul id="nav"&gt;
    {# using 'nav' and 'children' will create a recursive loop #}
    {% nav category in categories %}
        &lt;li&gt;
            {% if category.level == 1 %}
                &lt;a href="{{ entry.url }}" class="{{ category.slug }}" style="border-color: {{ category.supplierColour }}; color:{{ category.supplierColour }};" &gt;{{ category.title }}&lt;/a&gt;
            {% else %}
                &lt;a href="{{ entry.url }}" class="{{ category.slug }}"&gt;{{ category.title }}&lt;/a&gt;
            {% endif %}
            {% ifchildren %}
                &lt;div class="sub{{ category.level }} {{ category.slug }}"&gt;
                    &lt;ul&gt;
                        {% children %}
                    &lt;/ul&gt;
                &lt;/div&gt;
            {% endifchildren %}
        &lt;/li&gt;
    {% endnav %}
&lt;/ul&gt;
</code></pre>

<p>If you only want a certain number of levels then you can use a level filter. I.e:</p>

<pre><code>{% set categories = craft.categories.group('navCategories').level('&lt; 3') %}
</code></pre>

<p><strong>Update:</strong> If level 1 categories are not the same as your landing pages then you can access the category through <code>entry.category.first</code>, and then category decendants using: <code>{% for category in category.descendants %}...{% endfor %}</code> or <code>{% nav category in category.descendants %}...{% endnav %}</code> using the recursive method above.</p>

<pre><code>&lt;ul id="nav"&gt;
    {% set entries = craft.entries.section('landingPages') %}
    {% for entry in entries %}
        {% set category = entry.category.first %}
        &lt;li&gt;
            &lt;a href="{{ entry.url }}" class="{{ category.slug }}" style="border-color: {{ category.supplierColour }}; color:{{ category.supplierColour }};" &gt;{{ entry.title }}&lt;/a&gt;
            &lt;ul class="sub {{ category.slug }}"&gt;
                {# using 'nav' and 'children' will create a recursive loop, if more levels are needed #}
                {% nav category in category.descendants %}
                    &lt;li&gt;
                        &lt;a href="{{ category.url }}" title="{{ category.title }}"&gt;{{ category.title }}&lt;/a&gt;
                        {% ifchildren %}
                            &lt;ul class="sub{{ category.level }}"&gt;
                                {% children %}
                            &lt;/ul&gt;
                        {% endifchildren %}
                    &lt;/li&gt;
                {% endnav %}
            &lt;/ul&gt;
        &lt;/li&gt;
    {% endfor %}
&lt;/ul&gt;
</code></pre>

<p>As a side-note: Keep in mind that you can also define css dynamically within the loop rather than defining it on the elements directly, if you wanted to.</p>

<pre><code>{% set css %}
    a.{{ category.slug }} { 
        border-color: {{ category.supplierColour }}; 
        color:{{ category.supplierColour }}; 
    } 
{% endset %}
{% includeCss css %}
</code></pre>

CC BY-SA 3.0
2014-10-30T15:48:24.203Z
If I was just looking for categories that would be easy.

The issue is there are "Landing Pages" which act as a window into each category. The Landing Page is then associated with a category which pulls in things like the category logo, colour.

What I need is for the navigation to list out the entries in the "Landing Page" section, then find which category they are associated with... and then list out all children of that category.

Hope that makes sense.

Here is the URL to the development site.

http://cicadaretail.neondigital.com/
Dominic Leonard
CC BY-SA 3.0
2014-10-30T16:37:50.453Z
That's why I mentioned the second part. I figured that you could put the two together yourself. But I have updated the answer with a full example.
Douglas McDonald
CC BY-SA 3.0
2014-10-30T16:46:08.050Z
Another thing to mention is that categories can have their own url and template files (see the Docs link @Douglas provided). In this case it could make sense to delete the "Landing Pages" section altogether and instead use the category group not only for the navigation (first part of this nice answer) but also for the "Landing Pages" content.
carlcs
CC BY-SA 3.0
2014-10-30T16:57:10.237Z
[Structures](http://buildwithcraft.com/docs/sections-and-entries) and [Entry Types](http://buildwithcraft.com/help/entry-type-templates) also work great for this as well, which you can also [build nav from](http://buildwithcraft.com/help/structure-nav).
Douglas McDonald
CC BY-SA 3.0
2014-10-30T15:09:21.470Z
2014-10-30T16:40:44.020Z
2014-10-30T16:40:44.020Z
Douglas McDonald
<p>I've been scratching my head over this for awhile and I can't seem to solve it. </p>

<p>What I'm after is the following.</p>

<p>I have various categories with numerous sub categories. I'd like to be able to display the sub categories of a category that is related to an entry which is how my navigation  is set up.</p>

<p>The code at the moment look like this...</p>

<pre><code>{% set entries = craft.entries.find({ section: 'landingPages' }) %}
   {% for entry in entries %}
       &lt;li&gt;&lt;a href="{{ entry.url }}" class="{% set colour = entry.category.find() %}{% for category in colour %}{{ category.slug|striptags }}{% endfor %}" {% set colourTwo = entry.category.find() %}
           {% for category in colourTwo %} style="border-color: {{ category.supplierColour }}; color:{{ category.supplierColour }};" {% endfor %}&gt;{{ entry.title }}&lt;/a&gt;
           &lt;div class="sub {% set sub = entry.category.find() %}{% for category in sub %}{{ category.slug|striptags }}{% endfor %}"&gt;
           {% set drop = entry.category.find({ }) %}
               &lt;ul&gt;
               {% for category in drop %}
                  &lt;li&gt;&lt;a href="{{ category.url }}" title="{{ category.title }}"&gt;{{ category.title }}&lt;/a&gt;&lt;/li&gt;
               {% endfor %}
               &lt;/ul&gt;
           &lt;/div&gt;
        &lt;/li&gt;
    {% endfor %}
</code></pre>

<p>At the moment it's just generating the category which is selected within the entry, i've tried various variations on descendant commands and nothing seems to work.</p>

<p>Any help would be greatly appreciated.</p>

CC BY-SA 3.0
2014-10-30T05:29:28.293Z
I am not exactly sure what you are trying to do. Are you trying to get the children of the related category in the natural category heirarchy? or are you trying to get descendent categories that are also assigned to the entry?
Douglas McDonald
CC BY-SA 3.0
2014-10-30T05:30:18.073Z
You also seem to be doing a lot of extra work here that is not needed. If your initial entry only has one category assigned (which I assume from the above code that it does) then you can simply use: `{% set category = entry.category.first %}` and then access all of your category fields using `{{ category.slug }}`, `{{ category.title }}`,`{{ category.colour }}`, etc. etc. You do not need to loop through categories every time you want to grab a field value.
Douglas McDonald
CC BY-SA 3.0
2014-10-30T09:05:45.013Z
Cheers Douglas.

Yeah i'm not the most efficient in my twigging.

And yes i'm trying to get the children on the related category.
Dominic Leonard
<categories><relations>
Listing Sub Categories of a Category related to an entry
# Listing Sub Categories of a Category related to an entry

(Asked by: username_0 on 2014-10-29)

I've been scratching my head over this for awhile and I can't seem to solve it. 
What I'm after is the following.
I have various categories with numerous sub categories. I'd like to be able to display the sub categories of a category that is related to an entry which is how my navigation  is set up.
The code at the moment look like this...
{% set entries = craft.entries.find({ section: 'landingPages' }) %}
   {% for entry in entries %}
       {{ entry.title }}
           
           {% set drop = entry.category.find({ }) %}
               
               {% for category in drop %}
                  {{ category.title }}
               {% endfor %}
               
           
        
    {% endfor %}

At the moment it's just generating the category which is selected within the entry, i've tried various variations on descendant commands and nothing seems to work.
Any help would be greatly appreciated.

**Tags:** categories, relations
> **Comments:**
> **username_1**: I am not exactly sure what you are trying to do. Are you trying to get the children of the related category in the natural category heirarchy? or are you trying to get descendent categories that are also assigned to the entry?  
> **username_1**: You also seem to be doing a lot of extra work here that is not needed. If your initial entry only has one category assigned (which I assume from the above code that it does) then you can simply use: `{% set category = entry.category.first %}` and then access all of your category fields using `{{ category.slug }}`, `{{ category.title }}`,`{{ category.colour }}`, etc. etc. You do not need to loop through categories every time you want to grab a field value.  
> **username_0**: Cheers Douglas.  Yeah i'm not the most efficient in my twigging.  And yes i'm trying to get the children on the related category.  

---

## 1 answers

### Answer by username_1 on 2014-10-30. Score: 6 | Accepted answer

Check out the documentation for craft.categories which should do what you need. In this example, I assume that 'landing pages' are the same as your level 1 categories, in which case you don't need to deal with the entries at all:
{% set categories = craft.categories.group('navCategories') %}


    {# using 'nav' and 'children' will create a recursive loop #}
    {% nav category in categories %}
        
            {% if category.level == 1 %}
                {{ category.title }}
            {% else %}
                {{ category.title }}
            {% endif %}
            {% ifchildren %}
                
                    
                        {% children %}
                    
                
            {% endifchildren %}
        
    {% endnav %}


If you only want a certain number of levels then you can use a level filter. I.e:
{% set categories = craft.categories.group('navCategories').level('< 3') %}

Update: If level 1 categories are not the same as your landing pages then you can access the category through entry.category.first, and then category decendants using: {% for category in category.descendants %}...{% endfor %} or {% nav category in category.descendants %}...{% endnav %} using the recursive method above.

    {% set entries = craft.entries.section('landingPages') %}
    {% for entry in entries %}
        {% set category = entry.category.first %}
        
            {{ entry.title }}
            
                {# using 'nav' and 'children' will create a recursive loop, if more levels are needed #}
                {% nav category in category.descendants %}
                    
                        {{ category.title }}
                        {% ifchildren %}
                            
                                {% children %}
                            
                        {% endifchildren %}
                    
                {% endnav %}
            
        
    {% endfor %}


As a side-note: Keep in mind that you can also define css dynamically within the loop rather than defining it on the elements directly, if you wanted to.
{% set css %}
    a.{{ category.slug }} { 
        border-color: {{ category.supplierColour }}; 
        color:{{ category.supplierColour }}; 
    } 
{% endset %}
{% includeCss css %}

> **Comments:**
> **username_0**: If I was just looking for categories that would be easy.  The issue is there are "Landing Pages" which act as a window into each category. The Landing Page is then associated with a category which pulls in things like the category logo, colour.  What I need is for the navigation to list out the entries in the "Landing Page" section, then find which category they are associated with... and then list out all children of that category.  Hope that makes sense.  Here is the URL to the development site.  http://cicadaretail.neondigital.com/  
> **username_1**: That's why I mentioned the second part. I figured that you could put the two together yourself. But I have updated the answer with a full example.  
> **username_2**: Another thing to mention is that categories can have their own url and template files (see the Docs link @Douglas provided). In this case it could make sense to delete the "Landing Pages" section altogether and instead use the category group not only for the navigation (first part of this nice answer) but also for the "Landing Pages" content.  
> **username_1**: [Structures](http://buildwithcraft.com/docs/sections-and-entries) and [Entry Types](http://buildwithcraft.com/help/entry-type-templates) also work great for this as well, which you can also [build nav from](http://buildwithcraft.com/help/structure-nav).  

---

