<p>I think you don't need or want the <code>{% nav %}</code> tag. This should work with your current nav:
</p>

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

&lt;ul id="sideNav"&gt;
  {% for category in categories %}
    {% set thisIsActiveCategory = (category.slug == craft.request.segment(2)) %}
    &lt;li{% if thisIsActiveCategory %} class="active"{% endif %}&gt;
      {{ category.title }}
      {% if thisIsActiveCategory %}
        {# loop over the subcategories #}
        {% set subCategories = craft.categories.descendantOf(category) %}
        &lt;ul&gt;
          {% for subCat in subCategories %}
            &lt;li&gt;{{subCat.title}}&lt;/li&gt;
          {% endfor %}
        &lt;/ul&gt;
      {% endif %}
    &lt;/li&gt;
  {% endfor %}
&lt;/ul&gt;
</code></pre>

CC BY-SA 3.0
2014-07-03T23:44:17.360Z
Marion, I'm going to try this on Monday. Looks a lot closer than what I have been working on. Thanks!
Steph
CC BY-SA 3.0
2014-07-03T21:27:53.027Z
2014-07-03T21:27:53.027Z
Marion Newlevant
<p>The outline of what you want to do looks reasonable. Here are some details:
</p>

<pre><code>{# get the category from the segment #}
{% set parentCategory = craft.categories.slug(craft.request.segment(2)) %}

{# get the children of this category #}
{% set subCategories = craft.categories.descendantOf(parentCategory) %}

{# loop over the subcategories #}
{% for subCat in subCategories %}
  &lt;a href="{{subCat.url}}"&gt;{{subCat.title}}&lt;/a&gt;
{% endfor %}
</code></pre>

CC BY-SA 3.0
2014-07-03T19:05:23.127Z
Thanks for your answer Marion. I'm trying to apply this to my current nav setup, which I'll edit into my original question. So far no success, but will keep trying.
Steph
CC BY-SA 3.0
2014-07-03T18:25:34.877Z
2014-07-03T18:25:34.877Z
Marion Newlevant
<p>I'm currently using Categories to organize an inventory of products. The hierarchy is straightforward: category, subcategory, and single product entry. The url structure I'm are hoping to use reflects the hierarchy: <em>homepage.com/products/category/subcategory/product</em></p>

<p>So far, I've been able to <a href="https://craftcms.stackexchange.com/questions/353/how-do-i-add-a-subcategory-to-the-url-slug-for-a-channel-entry">display the proper slug for the products section</a> and <a href="https://craftcms.stackexchange.com/questions/4/how-do-you-list-categories-in-a-category-group">output a simple nav menu</a>. What I'd like to do now is refine the nav so that it only display the subcategories (children) of the category (parent) of product I'm on. I think I'm looking for <a href="https://craftcms.stackexchange.com/questions/590/how-do-you-only-show-children-of-active-parent-in-a-structure-sections-nav">something like this</a> but for Categories + Channel section rather than a Structure section.</p>

<p>I have an idea of how to achieve this, but I'm having trouble getting it to work. I think what I'd like to do is pull the url segment, match it to a category, then display its children. I know I'm missing something basic or haven't fully grasped the syntax of Twig. Any help would be appreciated, thank you!</p>

<p>-- edit --</p>

<p>Marion's suggestion was right on:</p>

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

&lt;ul id="sideNav"&gt;
  {% for category in categories %}
    {% set activeCat = (category.slug == craft.request.segment(2)) %}
    &lt;li{% if activeCat %} class="active"{% endif %}&gt;
      &lt;a href="{{category.url}}"&gt;{{ category.title }}&lt;/a&gt;
      {% if activeCat %}
        {% set subCategories = craft.categories.descendantOf(category) %}
        &lt;ul&gt;
          {% for subCat in subCategories %}
            &lt;li&gt;&lt;a href="{{subCat.url}}"&gt;{{subCat.title}}&lt;/a&gt;&lt;/li&gt;
          {% endfor %}
        &lt;/ul&gt;
       {% endif %}
   &lt;/li&gt;
  {% endfor %}
&lt;/ul&gt;
</code></pre>

<categories>
How do you only show children of “active” parent using categories?
# How do you only show children of “active” parent using categories?

(Asked by: username_0 on 2014-07-03)

I'm currently using Categories to organize an inventory of products. The hierarchy is straightforward: category, subcategory, and single product entry. The url structure I'm are hoping to use reflects the hierarchy: homepage.com/products/category/subcategory/product
So far, I've been able to display the proper slug for the products section and output a simple nav menu. What I'd like to do now is refine the nav so that it only display the subcategories (children) of the category (parent) of product I'm on. I think I'm looking for something like this but for Categories + Channel section rather than a Structure section.
I have an idea of how to achieve this, but I'm having trouble getting it to work. I think what I'd like to do is pull the url segment, match it to a category, then display its children. I know I'm missing something basic or haven't fully grasped the syntax of Twig. Any help would be appreciated, thank you!
-- edit --
Marion's suggestion was right on:
{% set categories = craft.categories.group('products').level('1') %}


  {% for category in categories %}
    {% set activeCat = (category.slug == craft.request.segment(2)) %}
    
      {{ category.title }}
      {% if activeCat %}
        {% set subCategories = craft.categories.descendantOf(category) %}
        
          {% for subCat in subCategories %}
            {{subCat.title}}
          {% endfor %}
        
       {% endif %}
   
  {% endfor %}

**Tags:** categories

---

## 2 answers

### Answer by username_1 on 2014-07-03. Score: 4 | Accepted answer

I think you don't need or want the {% nav %} tag. This should work with your current nav:

{% set categories = craft.categories.group('products') %}


  {% for category in categories %}
    {% set thisIsActiveCategory = (category.slug == craft.request.segment(2)) %}
    
      {{ category.title }}
      {% if thisIsActiveCategory %}
        {# loop over the subcategories #}
        {% set subCategories = craft.categories.descendantOf(category) %}
        
          {% for subCat in subCategories %}
            {{subCat.title}}
          {% endfor %}
        
      {% endif %}
    
  {% endfor %}

> **Comments:**
> **username_0**: Marion, I'm going to try this on Monday. Looks a lot closer than what I have been working on. Thanks!  

---

### Answer by username_1 on 2014-07-03. Score: 2

The outline of what you want to do looks reasonable. Here are some details:

{# get the category from the segment #}
{% set parentCategory = craft.categories.slug(craft.request.segment(2)) %}

{# get the children of this category #}
{% set subCategories = craft.categories.descendantOf(parentCategory) %}

{# loop over the subcategories #}
{% for subCat in subCategories %}
  {{subCat.title}}
{% endfor %}

> **Comments:**
> **username_0**: Thanks for your answer Marion. I'm trying to apply this to my current nav setup, which I'll edit into my original question. So far no success, but will keep trying.  

---

