how to display tags in a specific category? #5310
Replies: 3 comments
-
You can display tags for a specific category by first filtering posts in that category, then collecting and counting their tags. Here’s one way to do it in Liquid: {% assign category_posts = site.posts | where: "category", "your-category-name" %}
{% assign tags_list = "" | split: "" %}
{% for post in category_posts %}
{% assign tags_list = tags_list | concat: post.tags %}
{% endfor %}
{% assign tag_groups = tags_list | group_by: "to_s" | sort: "size" | reverse %}
<ul>
{% for tag in tag_groups %}
<li>
<a href="{{ site.baseurl }}/tag/{{ tag.name | slugify }}/">
{{ tag.name }} ({{ tag.size }})
</a>
</li>
{% endfor %}
</ul>
How it works:
This snippet first filters the posts so we’re only looking at ones in the chosen category. It then collects all their tags, groups identical tags together, and counts how many times each appears. Finally, it loops through the results and outputs a list of tags with links, similar to the main tags.html page.
|
Beta Was this translation helpful? Give feedback.
-
Hi thanks for the reply.
It would be great to have a layout similar to tags, where a summary/tally appears at the top of the page, but restricted to showing entries from the 'Notes' category only. Thanks again |
Beta Was this translation helpful? Give feedback.
-
Thanks for sharing your code 🙌 I think the issue is with how categories are being filtered. In your front matter you’ve got: categories:
- Notes That makes {% assign category_posts = site.posts | where: "category", "Notes" %} won’t match anything (because there’s no Instead, you can use {% assign category_posts = site.posts | where_exp: "post", "post.categories contains 'Notes'" %} Here’s a complete working ---
layout: archive
---
{% assign category_posts = site.posts | where_exp: "post", "post.categories contains 'Notes'" %}
{% assign tags_list = "" | split: "" %}
{% for post in category_posts %}
{% assign tags_list = tags_list | concat: post.tags %}
{% endfor %}
{% assign tag_groups = tags_list | group_by: "to_s" | sort: "size" | reverse %}
{% if tag_groups.size > 0 %}
<h2 class="archive__subtitle">Tags in Notes</h2>
<ul class="taxonomy__index">
{% for tag in tag_groups %}
<li>
<a href="{{ site.baseurl }}/tag/{{ tag.name | slugify }}/">
<strong>{{ tag.name }}</strong>
<span class="taxonomy__count">{{ tag.size }}</span>
</a>
</li>
{% endfor %}
</ul>
{% endif %}
<div class="entries-list">
{% for post in category_posts %}
{% include archive-single.html %}
{% endfor %}
</div> ✅ This does two things:
That should give you the “category-specific tags summary” experience you’re looking for. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm using the Minimal Mistakes theme and trying to create a category-specific page that displays a list of all tags associated with posts in that category, along with a tally for each tag (like the tags.html layout). is there a way to do this?
Beta Was this translation helpful? Give feedback.
All reactions