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

**URL:** <https://discourse.gohugo.io/t/count-number-of-pages-matching-a-term-in-a-page-of-kind-term-taxonomy-html/35376>\
**Category:** support\
**Tags:** taxonomy\
**Created:** [October 30, 2021, 11:30am UTC](https://discourse.gohugo.io/t/count-number-of-pages-matching-a-term-in-a-page-of-kind-term-taxonomy-html/35376 "2021-10-30T11:30:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![susana](https://avatars.discourse-cdn.com/v4/letter/s/9dc877/32.png) [@susana](https://discourse.gohugo.io/u/susana)\
**Post date:** [October 30, 2021, 11:31am UTC](https://discourse.gohugo.io/t/count-number-of-pages-matching-a-term-in-a-page-of-kind-term-taxonomy-html/35376/1 "2021-10-30T11:31:00Z")

</div>

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](https://gohugo.io/templates/taxonomy-templates/#taxonomy-methods)):

```auto
	{{ 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!

---

<div class="post-metadata">

**Author:** ![susana](https://avatars.discourse-cdn.com/v4/letter/s/9dc877/32.png) [@susana](https://discourse.gohugo.io/u/susana)\
**Post date:** [October 30, 2021, 12:06pm UTC](https://discourse.gohugo.io/t/count-number-of-pages-matching-a-term-in-a-page-of-kind-term-taxonomy-html/35376/2 "2021-10-30T12:06:07Z")

</div>

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.

```auto
# **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`.

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [October 30, 2021, 4:27pm UTC](https://discourse.gohugo.io/t/count-number-of-pages-matching-a-term-in-a-page-of-kind-term-taxonomy-html/35376/3 "2021-10-30T16:27:53Z")

</div>

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

```nohighlight
<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:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/gohugo/original/2X/0/06c1cf5b8fab3af07f2cb4ab9463550a128d2931.png)

---

<div class="post-metadata">

**Author:** ![susana](https://avatars.discourse-cdn.com/v4/letter/s/9dc877/32.png) [@susana](https://discourse.gohugo.io/u/susana)\
**Post date:** [October 30, 2021, 4:55pm UTC](https://discourse.gohugo.io/t/count-number-of-pages-matching-a-term-in-a-page-of-kind-term-taxonomy-html/35376/4 "2021-10-30T16:55:14Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [October 30, 2021, 5:31pm UTC](https://discourse.gohugo.io/t/count-number-of-pages-matching-a-term-in-a-page-of-kind-term-taxonomy-html/35376/5 "2021-10-30T17:31:18Z")

</div>

> [@susana](#):
>
> Could you please confirm if the below description is correct:

Yes, you are correct.

> [@susana](#):
>
> I think different websites (and even within websites) use terms and taxonomy inconsistently

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:

- [https://github.com/gohugoio/hugo/releases/tag/v0.73.0](https://github.com/gohugoio/hugo/releases/tag/v0.73.0)
- [https://github.com/gohugoio/hugo/pull/7394](https://github.com/gohugoio/hugo/pull/7394)

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](https://gohugo.io/templates/lookup-order/#examples-layout-lookup-for-term-pages) falls back to layouts/\_default/taxonomy.html if it cannot find layouts/\_default/term.html.

---

<div class="post-metadata">

**Author:** ![susana](https://avatars.discourse-cdn.com/v4/letter/s/9dc877/32.png) [@susana](https://discourse.gohugo.io/u/susana)\
**Post date:** [October 30, 2021, 5:38pm UTC](https://discourse.gohugo.io/t/count-number-of-pages-matching-a-term-in-a-page-of-kind-term-taxonomy-html/35376/6 "2021-10-30T17:38:01Z")

</div>

Thank you for your time!!
