<p>Something like this should work:</p>

<pre><code>{% set tags = craft.tags.limit(null).relatedTo(craft.entries.limit(null)).order('title') %}

{% for tag in tags %}
    &lt;a href="/tags/{{ tag.slug }}"&gt;{{ tag.title }} (craft.entries.relatedTo(tag).total())&lt;/a&gt;
{% endfor %}
</code></pre>

<p>Note that if you have a significant amount of tags, the above could be a costly affair, performance wise – <a href="https://craftcms.stackexchange.com/a/884/1098">consider alternative approaches</a>.</p>

CC BY-SA 3.0
2016-12-26T16:36:41.640Z
2016-12-26T16:36:41.640Z
2017-04-13T12:51:00.200Z
Mats Mikkel Rummelhoff
<p>I want to be able to display the number of entries that have a tag. Like:</p>

<p>Fruit (9)
Vegetables (4)</p>

<p>I also want to avoid displaying any tags without any related entries.</p>

<p>My code right now looks like this:</p>

<pre><code>{% extends "_layout" %}
{% set title = "Tags" %}
{% block content %}
{% for tag in craft.tags.order('title') %}
    &lt;li&gt;}&lt;a href="{{ siteUrl }}tags/{{ tag.title}}"&gt;{{ tag.title }}&lt;/a&gt;&lt;/li&gt;
{% endfor %}
{% endblock %}
</code></pre>

<tags><relatedto><count>
How to display number of entries for a tag and not display if no entries?
# How to display number of entries for a tag and not display if no entries?

(Asked by: username_0 on 2016-12-25)

I want to be able to display the number of entries that have a tag. Like:
Fruit (9)
Vegetables (4)
I also want to avoid displaying any tags without any related entries.
My code right now looks like this:
{% extends "_layout" %}
{% set title = "Tags" %}
{% block content %}
{% for tag in craft.tags.order('title') %}
    }{{ tag.title }}
{% endfor %}
{% endblock %}

**Tags:** tags, relatedto, count


---

## 1 answers

### Answer by username_1 on 2016-12-26. Score: 4 | Accepted answer

Something like this should work:
{% set tags = craft.tags.limit(null).relatedTo(craft.entries.limit(null)).order('title') %}

{% for tag in tags %}
    {{ tag.title }} (craft.entries.relatedTo(tag).total())
{% endfor %}

Note that if you have a significant amount of tags, the above could be a costly affair, performance wise – consider alternative approaches.


---

