Does anyone know how to get the number of articles for a certain tag?
I found a tag list with the according number behind it in the icarus template, but I would like to have some static tags, that I manually pick. That should be like “4 Articles in Climbing”, where Climbing is the tag.
The example showing all tags with their article count is this:
`{{ if not (eq (len .Site.Taxonomies.tags) 0) }}
{{ with .Site.Data.l10n.widgets.tags.title }}{{.}}{{end}}
{{ range $name, $items := .Site.Taxonomies.tags }}
after trying to combine template functions to count the articles tagged with xyz, I got this snippet working:
<ul>
<!-- Get all posts-->
{{ $posts := where .Site.Pages "Type" "post" }}
<!-- loop all tags that you've used -->
{{ range $name, $taxonomy := .Site.Taxonomies.tags }}
<!-- (re)set the counter to 0 -->
{{ $.Scratch.Set "tagCounter" 0 }}
<!-- increment counter if post contains the current tag-->
{{ range $posts}}
{{ if in .Params.tags $name }}
{{ $.Scratch.Add "tagCounter" 1 }}
{{ end }}
{{ end }}
<li>{{ $.Scratch.Get "tagCounter" }} articles in {{ $name }}</li>
{{end}}
</ul>
Maybe does anyone else know a better solution. But it should work for now.
However, there is one small issue. .Site.Taxonomies.tags contains all tags lowercased, even if you define a tag like tags = ["Programming"].
The problem is that the template function in is case-sensitive. If it looks for programming in tags = ["Programming"], it would return false and the counter will not be incremented. In other words: all tags you set in the frontmatter need to be lowercased.
Thanks for that code snipped! Just one last bit is missing for me. Ho do I in the end specify one certain tag? Now I get a list with all tags and their acticle (post) count, but I didn’t get the syntax to limit this.
I feel like I need to do that at this part, because there I define the range of tags I want to consider.
{{ range $name, $taxonomy := .Site.Taxonomies.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>