How to list tags for the current post - Eleventy js?

Viewed 203

I am using Eleventy JS. And I can't list the tags for the current post. There is nothing written about this in the official documentation.

All tags are listed easily. But for the current post I can not display the tags on the screen

This code to list all tags in Eleventy JS config file

      // Display tag list on page
  eleventyConfig.addCollection('tagsList', function (collectionApi) {
    const tagsList = new Set()
    collectionApi.getAll().map((item) => {
      if (item.data.tags) {
        // handle pages that don't have tags
        item.data.tags.filter((tag) => !['posts'].includes(tag)).map((tag) => tagsList.add(tag))
      }
    })
    return tagsList
  })

This loop of outputting tags in the template

---
permalink:  /blog/category/

pagination:
  data: collections
  size: 77
---

<h1>Tags</h1>

<ul>
    {% for tag in collections.tagsList %}
        <a href="/blog/category/{{tag}}">{{tag}}</a>
    {% endfor %}
</ul>

I use Nunjucks

1 Answers

Displaying tags for the current post with filter:

{% for tag in tags -%}
    {% if tag !== 'posts' %}
        <a class="tag" href="/blog/category/{{ tag }}">{{ tag }}</a>
    {% endif %}
{%- endfor %}
Related