How to display category and tag counts in Jekyll | Dafacto
Dafacto

The personal website of Matt Henderson.

How to display category and tag counts in Jekyll

15 September 2023

After two decades, I recently migrated my blog from WordPress to Jekyll, a static website generator. I’ll write another article soon on why and the process, but as I continue to tweak the site, I wanted to quickly post how to do something that was surprisingly difficult to find.

On my Guide page, I wanted to link to the index pages of my categories and tags, and in addition, I wanted to show the count of each.

This article describes how to bring generate tag and categories index pages, but I couldn’t find any articles describing how to display how many articles were in each tag or category.

After some trial and error, here’s the code that finally works:

	

	{% assign sorted_cats = site.categories | sort %}
	{% for category in sorted_cats %}[{{ category[0] }}](/category/{{ category[0] }}/) ({{category[1].size }}) {% endfor %}

	{% assign sorted_tags = site.tags | sort %}
	{% for tag in sorted_tags %}[{{ tag[0] }}](/tag/{{ tag[0] }}/) ({{tag[1].size }}) {% endfor %}

	
	

Enjoy this article? — You can find similar content via the category and tag links below.

Questions or comments? — Feel free to email me using the contact form below, or reach out on Twitter.