How do you only show children of “active” parent using categories? 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') %}

&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;
I think you don't need or want the {% nav %} tag. This should work with your current nav:


{% 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;