Show count of handpicked categories or tags

If you just want to count the posts of a single tag, try this snippet:

<ul>
        <!-- Get all posts-->
        {{ $posts := where .Site.Pages "Type" "post" }}

	<!-- (re)set the counter to 0 -->
	{{ $.Scratch.Set "tagCounter" 0 }}
	<!-- increment counter if post contains the current tag-->
	{{ range $posts}}
		{{ if in .Params.tags "TAGNAME" }}
			{{ $.Scratch.Add "tagCounter" 1 }}
		{{ end }}
	{{ end }}
	
	<li>{{ $.Scratch.Get "tagCounter" }} articles tagged with TAGNAME</li>
</ul>

Replace TAGNAME with your single tag.

4 Likes