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') %}
    <li>}<a href="{{ siteUrl }}tags/{{ tag.title}}">{{ tag.title }}</a></li>
{% endfor %}
{% endblock %}

Something like this should work:

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

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.