<p>Of course after posting my question I immediately <a href="https://craftcms.stackexchange.com/questions/718/how-do-you-only-show-children-of-active-parent-using-categories">find an answer</a>!!!</p>

<p>Would be good if someone can confirm this is the best way?</p>

<pre><code>      {% set categories = craft.categories.group('areaOfPractice') %}
        {% for category in categories %}
          {% set thisIsActiveCategory = (category.slug == craft.request.segment(2)) %}
            {% if thisIsActiveCategory %}
              {% 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 %}
        {% endfor %}
</code></pre>

CC BY-SA 3.0
2015-08-29T14:11:51.727Z
2015-08-29T14:11:51.727Z
2017-04-13T12:51:00.160Z
Martin
<p>You should actually already have a variable named "category"
pre-assigned by Craft, if you've set up your categories to have URLs in the category settings.</p>

<p>So this simplified code is probably all you need.</p>

<pre><code>{% set subCategories = craft.categories.descendantOf(category) %}

{% if subCategories|length %}
    &lt;ul&gt;
        {% for category in subCategories %}
            &lt;li&gt;&lt;a href="{{ category.url }}"&gt;{{ category.title }}&lt;/a&gt;&lt;/li&gt;
        {% endfor %}
    &lt;/ul&gt;
{% else %}
    {% exit 404 %}    
{% endif %}
</code></pre>

<p>To have different template code for your sub-category pages you're linking to, I would
put the code above in a partial _partials/mainCategory and include it with the <code>include</code> function.</p>

<pre><code>{% if category.level == 1 %}
    {% include '_partials/mainCategory' %}
{% else %}
    {% include '_partials/subCategory' %}
{% endif %}
</code></pre>

CC BY-SA 3.0
2015-08-29T18:59:06.420Z
2015-08-29T18:59:06.420Z
carlcs
<p>I have a category group with parents each with 1 level of children.</p>

<p>On my category entry page I want to list JUST the children of the current category being viewed.</p>

<p>I'm sure it's simple but I can't figure what to put in my category template?</p>

<p>Many thanks in advance.</p>

<p>Martin</p>

<categories>
Showing just the child category titles of a parent
# Showing just the child category titles of a parent

(Asked by: username_0 on 2015-08-29)

I have a category group with parents each with 1 level of children.

On my category entry page I want to list JUST the children of the current category being viewed.

I'm sure it's simple but I can't figure what to put in my category template?

Many thanks in advance.

Martin

**Tags:** categories

---

## 2 answers

### Answer by username_1 on 2015-08-29. Score: 5 | Accepted answer

You should actually already have a variable named "category"
pre-assigned by Craft, if you've set up your categories to have URLs in the category settings.
So this simplified code is probably all you need.
{% set subCategories = craft.categories.descendantOf(category) %}

{% if subCategories|length %}
    
        {% for category in subCategories %}
            {{ category.title }}
        {% endfor %}
    
{% else %}
    {% exit 404 %}    
{% endif %}

To have different template code for your sub-category pages you're linking to, I would
put the code above in a partial _partials/mainCategory and include it with the include function.
{% if category.level == 1 %}
    {% include '_partials/mainCategory' %}
{% else %}
    {% include '_partials/subCategory' %}
{% endif %}


---

### Answer by username_0 on 2015-08-29. Score: 1

Of course after posting my question I immediately find an answer!!!
Would be good if someone can confirm this is the best way?
      {% set categories = craft.categories.group('areaOfPractice') %}
        {% for category in categories %}
          {% set thisIsActiveCategory = (category.slug == craft.request.segment(2)) %}
            {% if thisIsActiveCategory %}
              {% set subCategories = craft.categories.descendantOf(category) %}
              
                {% for subCat in subCategories %}
                {{ subCat.title }}
                {% endfor %}
              
            {% endif %}
        {% endfor %}


---

