Resources
Resources
Displaying Tags that are In Use
Once you create a tag, it’s in the system whether you delete it later or not. Because of that, outputting a craft.tags()
list might include tags that are not actually in use anywhere.
To output a list of tags that are “in use”, you must first identify what “in use” means. Do you want the tags that are selected by any entry? Or just the tags that are are selected by your News entries? Whatever it is, start by creating a variable that identifies those entries, using craft.entries()
:
{% set entries = craft.entries()
.section('news')
.limit(null)
.all()
%}
From there it’s just a matter of grabbing the tags that are related to those entries:
<ul>
{% for tag in craft.tags().relatedTo(entries) %}
<li>{{ tag.title }}</li>
{% endfor %}
</ul>