How do I get tags to only display on posts that have that tag? I’m trying to add tags to my blog posts.

I’ve created a tags group with the handle ‘default’.

In Setting/Fields I’ve got a field called bodyElements which is setup as a matrix, and within bodyElements I’ve added to the Configuration a block type called Tags with a handle of ‘tags’, and then within that there’s a field called Tags with a handle of ‘blogTags’ and this is set as the Field Type of “Tags” and the Source is set as “Tags”.

Now when I go to a blog post, I can type in my content and click the button to add a Tag. I can then type in a tag and it attaches to that particular entry.

The issue I’m having is that when I view the blog posts on the front end, the tags are showing up on every single post, even though they don’t have those tags attached to them.

On https://craftcms.com/support/active-tags it says to insert the following code to display only the tags that are in use

{% set entries = craft.entries.section('news').limit(null) %}
&lt;ul&gt;
    {% for tag in craft.tags.relatedTo(entries) %}
        &lt;li&gt;{{ tag.title }}&lt;/li&gt;
    {% endfor %}
&lt;/ul&gt;


but that doesn’t display the tags at all.

My blog section has a handle of ‘blog’, so in the above code I tried replacing ‘news’ with ‘blog’ and still no tags appear.

The only way I can get tags to appear on my posts is by using the following code:

{% for tag in craft.tags.group(‘default’) %}
    &lt;li&gt;&lt;a href="{{ url('blog/tags/'~tag.id) }}"&gt;{{ tag.title }}&lt;/a&gt;&lt;/li&gt;
{% endfor %}


but the above code caused the tags to appear on every single blog post, even if they’re not tagged with anything.

How do I get the tags to only appear on the posts the tags are attached to?

Thanks.
The code you're using at the moment is trying to output the tags that are in use on any/all entries:

{% set entries = craft.entries.section('news').limit(null) %}


If I understand you correctly, you want to output the tags only for the current entry, right? 

This is explained with very useful examples in the documentation.

Solution

Assuming you already have the entry variable set, you could do the following:

&lt;ul&gt;
    {% for tag in craft.tags.relatedTo(entry) %}
        &lt;li&gt;{{ tag.title }}&lt;/li&gt;
    {% endfor %}
&lt;/ul&gt;


Another example adapted from the docs:

{% set tags = entry.tagsFieldHandle %}

{% if tags | length %}

    &lt;ul&gt;
        {% for tag in tags %}
            &lt;li&gt;{{ tag.title }}&lt;/li&gt;
        {% endfor %}
    &lt;ul&gt;

{% endif %}


You first set the variable tags to the tags related to your entry. Then, you check if there are tags related to the entry. If there are, you iterate over them.