Count of posts in category taxonomy terms list

In categories.terms.html, I am trying to display a count of posts associated with it. However, I get an error stating can't evaluate field Count in type *hugolib.Page. What am I doing wrong?

{{ define "main" }}
  {{ range .Data.Pages }}
    <div class="container">
      <div class="row">
        <div class="col-sm-8 col-sm-push-2">
          <article class="categories">
            <h2 class="item-title">{{ .Title }}</h2>
            <p>{{ .Content }}</p>
            <div class="flex-wrap always-flex">
              <div class="flex-left">
                <a href="/categories/{{ .Title | urlize }}/" class="btn">{{ .Count }} blogs</a>
              </div>
            </div>
          </article>
        </div>
      </div>
    </div>
  {{ end }}
{{ end }}

Please read over requesting help, and share more information, including a link to your site’s source code, and full error messages.

Don’t know anything about .Count and that error message says that it cannot be evaluated in Hugo.

To get the posts count we use len see the example at: https://gohugo.io/functions/len/#len-example-2-counting-pages-with-where

Thanks, that’s useful. I tried {{ $posts := (where .Site.Taxonomies "categories" "==" $term) }} {{ $postCount := len $posts }} where {{ $term := urlize .Title }}. However, that returned 0 results.

I went with this in the end:

{{ $title := urlize .Title }}
{{ with .Site.Taxonomies.categories }}
  {{ range $name, $taxonomy := . }}
    {{ $term := urlize $name }}
    {{ if eq $term $title }}
      <div class="flex-wrap always-flex">
        <div class="flex-left">            
          <a href="/categories/{{ $name | urlize }}/" class="btn">{{ $taxonomy.Count }} blogs</a>
        </div>
      </div>
    {{ end }}
  {{ end }}
{{ end }}
1 Like