Count number of pages matching a term in a page of kind=term (taxonomy.html)

In a page of .Kind=term (in layouts/_default/taxonomy.html) which lists all pages associated with a given term (e.g.: pages with category=css) I wish to present the number of pages with the term.
Example of what the page would contain:

Blog posts published in the category: css

There are 2 posts with category "css":

  1. 2021/10/14: CSS is really great
  2. 2021/10/01: CSS is great

I want to be able to count the total number of pages with the term to build the sentence: “There are 2 posts”.

I have tried this (from Taxonomy templates | Hugo):

	{{ range $taxonomy_term, $taxonomy := .Site.Taxonomies }}
	{{ with $.Site.GetPage (printf "/%s" $taxonomy_term) }}
	<p>{{ $taxonomy_term }}</p>
	{{ range $key, $value := $taxonomy }}
	<p>{{ $key }}</p>
	<p>{{len $value}}</p>
	{{ end }}
	{{ end }}
	{{ end }}

It works, but I need to add two conditionals:

  1. if $taxonomy := blog/categories or, preferably, the .Section/.Type of current page
  2. if taxonomy_term := the term associated with the current page (“css”) since this is all happening in .Kind=term page (taxonomy.html).

There is one important caveat, however: my taxonomy names (e.g. blog/categories) have special characters (“/”) so I cannot use function such as “.Site.Taxonomies.blog/categories”.

I have searched and tried different formulations to the functions, but don’t seem to get it right.
Thank you, as always!

The following code does what I need, i.e. count the number of posts with a given term (e.g.: category=css) before listing those posts. I am on page of kind=term in layouts/_default/taxonomy.html.

#**Run in: layouts/_default/taxonomy.html of .Kind=term**
#.Title=term (e.g.: css); .Section=taxonomy name (e.g.: blog/categories)

{{$term := .Title}}
{{$taxo := .Section}}
{{ range $taxonomy_term, $taxonomy := .Site.Taxonomies }}
   {{ if eq $taxonomy_term $taxo}}
      {{ with $.Site.GetPage (printf "/%s" $taxonomy_term) }}
         <p>taxonomy_term:{{ $taxonomy_term }}</p>
         {{ range $key, $value := $taxonomy }}
            {{ if eq $key $term}}
              <p>key:{{ $key }}</p>
              <p>len_key:{{len $value}}</p>
           {{ end }}
          {{ end }}
      {{ end }}
   {{ end }}
{{ end }}

Is there a more elegant and sustainable way of achieving the same result? I am only interested in len or Count.

First, for greater specificity and to avoid confusion, I suggest using layouts/_default/term.html for term pages, and layouts/_default/taxonomy.html for taxonomy pages.

Second, you can count the number of pages using the len function.

layouts/_default/term.html

<h1>{{ printf "Blog posts published in the %s: %s" .Data.Singular .Title }}</h1>
<p>{{ printf "There are %d posts with %s %q:" (len .Pages) .Data.Singular .Title }}</p>
<ol>
  {{ range .Pages }}
    <li><a href="{{ .RelPermalink }}">{{ .Title }}</a></li>
  {{ end }}
</ol>
{{ end }}

This produces:

I have read a lot but I think different websites (and even within websites) use terms and taxonomy inconsistently, and I am not going to mention that I find it impossible to make sense of the taxonomy templates lookup order.

Could you please confirm if the below description is correct:

  1. taxonomy pages: layouts/_default/taxonomy.html - this is meant to list all the terms of a taxonomy (e.g.: list all terms in categories: css, html, etc.). This page should have .Kind=taxonomy.
  2. term pages: layouts/_default/term.html - this is meant to list the pages with a term (e.g.: list all pages where categories=css). This page should have .Kind=term.

Thank you once more!

Yes, you are correct.

Yes, they do. We renamed page kinds last year, so some of what you have seen is probably outdated.

Description <= v0.72.0 Kind >= v0.73.0 Kind
List of terms within a taxonomy taxonomyTerm taxonomy
List of pages with a given term taxonomy term

References:

This change could have broken many sites, so accommodations were made to provide some degree of backwards compatibility. For exampe, the lookup order for term pages falls back to layouts/_default/taxonomy.html if it cannot find layouts/_default/term.html.

Thank you for your time!!