How to get the top term?

I would like to extract the Top Tag or Top Category in my content, namely the tag or category which has the highest number of pages. I need to do this in a generic page, so I can only rely on the .Site.Taxonomies variable.

The problem is that, when I query .Site.Taxonomies.tags or .Site.Taxonomies.categories I get a list of maps that does not include the term name!!!

{{ $terms := .Site.Taxonomies.tags }}
{{ range $terms }}
<span>{{ .Name }}</span><br/>
{{ end }}

This code throws an error as .Name is not defined. How can I achieve this?

{{ range first 1 $terms.ByCount }}

4 Likes

An alternative method to @ju52’s nice post.

{{ $terms := site.Taxonomies.tags.ByCount }}
{{ $top_term := index $terms 0 }}
2 Likes

How to display the name of the tag inside the range?

Should be as you had it beforehand:

{{ .Name }}