lektor / lektor-tags

For each tag on your site, build a list of pages with that tag. This can be used for standard tag-based blog navigation.
MIT License
27 stars 6 forks source link

How to get a list of tags? #12

Closed ericmjl closed 6 years ago

ericmjl commented 6 years ago

I'd like to create a page on my website that lists all of the tags that I have used. I'm not quite sure how to do this using lektor-tags, even though I have pored over the documentation quite a bit. Perhaps I'm not looking in the right place? Is there an example of how this can be accomplished?

ericmjl commented 6 years ago

I just figured it out! The plugin provided the starting point reminder for me to look at querying the database.

For the benefit of future users who might want a starting point, here's my code below.

{% set blogposts = site.query('/blog') %}
{% set tags = [] %}
{% for post in blogposts %}
  {% for tag in post.tags if tag not in tags %}
    <!-- {{ tags.append(tag) }} -->
  {% endfor %}
{% endfor %}

<ul>
  {% for tag in tags|sort %}
    <li><a href="{{ site.url_path }}/blog/tag/{{ tag }}">{{ tag }}</a></li>
  {% endfor %}
</ul>