<p>you can use .count() to determine if there is an empty tag:</p>

<pre><code>{% for tag in tags %}
{% set count = craft.entries.section('fellows').relatedTo(category).relatedTo(tag).count() %}
  {% if count &gt; 0 %}
     &lt;h3&gt;{{ tag.slug }} {{ category.title }}&lt;/h3&gt;
         {% for entry in craft.entries({
                                relatedTo: [
                                  'and',
                                  { targetElement: category },
                                  { targetElement: tag },
                                  ],
                                  limit: (null),
                                  }) %}
               {% include '_entryblock.twig' %} 
  {% endif %}
{% endfor %}
</code></pre>

CC BY-SA 4.0
2018-08-27T00:34:12.727Z
2018-08-27T00:34:12.727Z
Nikolay Bronskiy
<p>Here's a good tutorial on how to achieve this: <a href="https://straightupcraft.com/articles/how-do-i-only-display-a-list-of-categories-that-have-entries-and-hide-empty-categories" rel="nofollow noreferrer">https://straightupcraft.com/articles/how-do-i-only-display-a-list-of-categories-that-have-entries-and-hide-empty-categories</a></p>

CC BY-SA 4.0
2018-08-21T18:17:26.690Z
2018-08-21T18:17:26.690Z
Jay
<p>Entries can have one of two categories and one tag. 
(People = entries | program = categories | year = tag)</p>

<p>On the pages I'd like to display entries related to the category and grouped by tag. However, I don't want the tag to display if there are no entries with that tag and in that category. </p>

<p>The following code works, but it displays empty tags that I'd like hidden. </p>

<pre><code>{% set entryIds = craft.entries.section('fellows').ids() %}
{% set tags = craft.tags.relatedTo({ sourceElement: entryIds }).groupId(2).find() | reverse %}
   {% for tag in tags %}
      &lt;h3&gt;{{ tag.slug }} {{ category.title }}&lt;/h3&gt;
          {% for entry in craft.entries({
                            relatedTo: [
                              'and',
                              { targetElement: category },
                              { targetElement: tag },
                              ],
                              limit: (null),
                              }) %}
           {% include '_entryblock.twig' %} 
           {% endfor %} 
    {% endfor %} 
</code></pre>

<p>This currently displays the following — but I don't want it to display 2018 in Program A or 2017 in Program B since neither of those have entries. </p>

<p>Program A:</p>

<pre><code>/program-a (category)
  |__2018 (tag)
  |__2017
       |__Bono (entry)
       |__The Edge
  |__2016
       |__Ringo
       |__Paul  
</code></pre>

<p>Program B:</p>

<pre><code>/program-b
  |__2018
       |__Chris Martin
       |__Jonny Buckland
  |__2017
  |__2016
       |__Florence
       |__The Machine  
</code></pre>

<templating><categories><tags>
How do I hide tags in a category that have no entries?
# How do I hide tags in a category that have no entries?

(Asked by: username_0 on 2018-08-21)

Entries can have one of two categories and one tag. 
(People = entries | program = categories | year = tag)
On the pages I'd like to display entries related to the category and grouped by tag. However, I don't want the tag to display if there are no entries with that tag and in that category. 
The following code works, but it displays empty tags that I'd like hidden. 
{% set entryIds = craft.entries.section('fellows').ids() %}
{% set tags = craft.tags.relatedTo({ sourceElement: entryIds }).groupId(2).find() | reverse %}
   {% for tag in tags %}
      {{ tag.slug }} {{ category.title }}
          {% for entry in craft.entries({
                            relatedTo: [
                              'and',
                              { targetElement: category },
                              { targetElement: tag },
                              ],
                              limit: (null),
                              }) %}
           {% include '_entryblock.twig' %} 
           {% endfor %} 
    {% endfor %} 

This currently displays the following — but I don't want it to display 2018 in Program A or 2017 in Program B since neither of those have entries. 
Program A:
/program-a (category)
  |__2018 (tag)
  |__2017
       |__Bono (entry)
       |__The Edge
  |__2016
       |__Ringo
       |__Paul  

Program B:
/program-b
  |__2018
       |__Chris Martin
       |__Jonny Buckland
  |__2017
  |__2016
       |__Florence
       |__The Machine

**Tags:** templating, categories, tags

---

## 2 answers

### Answer by username_1 on 2018-08-27. Score: 1

you can use .count() to determine if there is an empty tag:
{% for tag in tags %}
{% set count = craft.entries.section('fellows').relatedTo(category).relatedTo(tag).count() %}
  {% if count > 0 %}
     {{ tag.slug }} {{ category.title }}
         {% for entry in craft.entries({
                                relatedTo: [
                                  'and',
                                  { targetElement: category },
                                  { targetElement: tag },
                                  ],
                                  limit: (null),
                                  }) %}
               {% include '_entryblock.twig' %} 
  {% endif %}
{% endfor %}


---

### Answer by username_2 on 2018-08-21. Score: 1

Here's a good tutorial on how to achieve this: <https://straightupcraft.com/articles/how-do-i-only-display-a-list-of-categories-that-have-entries-and-hide-empty-categories>


---

