Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-09-11T22:13:14.457Z
Answers.LastActivityDate: 2019-09-11T22:18:22.290Z
Answers.LastEditDate: 2019-09-11T22:18:22.290Z
Answers.UserDisplayName: kmgdev
Body: <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>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-09-11T16:47:37.217Z
Comments.Text: What is wrong with using the same query format to get the subcategories?
Comments.UserDisplayName: Clark Nelson
Tags: <templating><categories>
Title: List child categories which have entries based on the current categories
ThreadText: # 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.
  
  ```
  <ul>
      {# 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 %}
              <li><a href="{{ subCategory.url }}">{{ subCategory.title }}</a></li>
          {% endif %}
      {% endfor %}
  </ul>
  
  ```
  
  You'll need to provide the `parentCategoryObject` yourself based on however your templating works.
  
  
  ---
  
  