<p>Use the <code>descendantOf</code> parameter to select just the subcategories, then run an entries query to check if that category has any entries assigned to it.</p>

<pre><code>&lt;ul&gt;
    {# loop through the subcategories #}
    {% for subCategory in craft.categories({
        groupId: 2,
        descendantOf: parentCategoryObject,
        orderBy: 'title'
    }).all %}
        {# check if this subcategory has at least one entry assigned to it #}
        {% set subCatEntries = craft.entries({
            section: 'blog',
            relatedTo: subCategory,
            limit: 1
        }) %}

        {# display a link to the subcategory page if entries are assigned to it #}
        {% if subCatEntries|length %}
            &lt;li&gt;&lt;a href="{{ subCategory.url }}"&gt;{{ subCategory.title }}&lt;/a&gt;&lt;/li&gt;
        {% endif %}
    {% endfor %}
&lt;/ul&gt;
</code></pre>

<p>You'll need to provide the <code>parentCategoryObject</code> yourself based on however your templating works.</p>

CC BY-SA 4.0
2019-09-11T22:13:14.457Z
2019-09-11T22:18:22.290Z
2019-09-11T22:18:22.290Z
kmgdev
<p>I am looking to output the child categories of a parent category.
I only need to show the categories which have entries related to them.</p>

<p>I have done this to show all categories using</p>

<pre><code>{% set entryIds = craft.entries.section('stories').ids() %}
{% set categories = craft.categories.relatedTo({ sourceElement: entryIds }).groupId(2).orderBy('title').all() %}
</code></pre>

<p>What is the best way to do this for subcategories.</p>

CC BY-SA 4.0
2019-09-11T16:47:37.217Z
What is wrong with using the same query format to get the subcategories?
Clark Nelson
<templating><categories>
List child categories which have entries based on the current categories
# List child categories which have entries based on the current categories

(Asked by: username_0 on 2019-09-11)

I am looking to output the child categories of a parent category.
I only need to show the categories which have entries related to them.
I have done this to show all categories using
{% set entryIds = craft.entries.section('stories').ids() %}
{% set categories = craft.categories.relatedTo({ sourceElement: entryIds }).groupId(2).orderBy('title').all() %}

What is the best way to do this for subcategories.

**Tags:** templating, categories

> **Comments:**
> **username_1**: What is wrong with using the same query format to get the subcategories?  

---

## 1 answers

### Answer by username_2 on 2019-09-11. Score: 1

Use the descendantOf parameter to select just the subcategories, then run an entries query to check if that category has any entries assigned to it.

    {# loop through the subcategories #}
    {% for subCategory in craft.categories({
        groupId: 2,
        descendantOf: parentCategoryObject,
        orderBy: 'title'
    }).all %}
        {# check if this subcategory has at least one entry assigned to it #}
        {% set subCatEntries = craft.entries({
            section: 'blog',
            relatedTo: subCategory,
            limit: 1
        }) %}

        {# display a link to the subcategory page if entries are assigned to it #}
        {% if subCatEntries|length %}
            {{ subCategory.title }}
        {% endif %}
    {% endfor %}


You'll need to provide the parentCategoryObject yourself based on however your templating works.


---

