1

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) %}
<ul>
    {% for tag in craft.tags.relatedTo(entries) %}
        <li>{{ tag.title }}</li>
    {% endfor %}
</ul>

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’) %}
    <li><a href="{{ url('blog/tags/'~tag.id) }}">{{ tag.title }}</a></li>
{% 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.

1 Answer 1

2

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:

<ul>
    {% for tag in craft.tags.relatedTo(entry) %}
        <li>{{ tag.title }}</li>
    {% endfor %}
</ul>

Another example adapted from the docs:

{% set tags = entry.tagsFieldHandle %}

{% if tags | length %}

    <ul>
        {% for tag in tags %}
            <li>{{ tag.title }}</li>
        {% endfor %}
    <ul>

{% 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.

3
  • @Wayne Does this answer help you? If not, ask more questions. If so, please mark it as accepted! Commented Apr 11, 2017 at 13:19
  • Using the second example of your code I get the following error. Internal Server Error Craft\EntryModel and its behaviors do not have a method or closure named "tagsFieldHandle".
    – Wayne
    Commented Apr 11, 2017 at 13:48
  • That's because tagsFieldHandle is only an example. You should replace it with the handle of your tags field. At some point you created a field in the Craft control panel to select tags for your post, right? Use the handle of that field. Commented Apr 11, 2017 at 17:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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