Istvan
Istvan

Reputation: 8572

How to access a specific taxonomy in the page template?

I am trying to render the index.html with each entry listing the tags for each post.

_index.md:

---
title = "Posts"
sort_by = "date"
template = "posts/index.html"
page_template = "posts/page.html"
---

Example post:

taxonomies:
  tags:
    - aws
    - lambda
    - bun

posts/index.html:

{% if page.taxonomies.tags %}
   {{ post.taxonomies.tags | json_encode }}
{% else %}
   {{ [] | json_encode }}
{% endif %}  

Rendering index.html results in [] for all posts even though the taxonomy is enabled in config.toml. Is there anything else to configure to get the tags accessible for posts?

Upvotes: 1

Views: 46

Answers (1)

charlie137
charlie137

Reputation: 259

You need to get the post page first, then iterate over its tags:

{% set post_page = get_page(path=“posts/first/index.md”) %}
{% for tag in post_page.taxonomies.tags %}
  {{ tag }}
{% endfor %}

Upvotes: 0

Related Questions