List all the categories used in the blog

Hi,

I try to list all the categories used in my blog like this:

<ul class="categories">
{{range $name, $taxonomy := .Site.Taxonomies.categories}}
<li><a href="{{$baseurl}}{{"categories/" | relLangURL}}{{$name | urlize}}">{{$name}}</a></li>
{{end}}
</ul>

But I get two problems:

  1. The list works only when I restart $hugo server. When I refresh the page .Site.Taxonomies.categories is empty
  2. When it works, I only get the url of the category but not the name I defined in a _index.md (I can get the title of the categories with .Params.categories in a single page)

Any idea ? Thanks!

That’s odd. I just checked my theme and I’m listing categories pretty much in the same way

{{ range $key, $value := .Site.Taxonomies.categories }}
	<li><a href="{{ $baseurl  }}categories/{{ $key | urlize  }}">{{ $key | humanize }}</a> ({{ len $value }})</li>
{{ end }}

how is your config.toml set up?

Mine is like this:

[taxonomies]
tag = "tags"
category = "categories"

could this be a result of the default fast render mode?

1 Like

By trying to solve my second problem, I solved my first one.
I don’t use .Site.Taxonomies.categories anymore (which was empty most of the time).

{{range ($.Site.GetPage "taxonomyTerm" "categories").Pages }}
   <a href="{{.Permalink}}">{{.Title}}</a></li>
{{end}}

I’m still curious to understand why .Site.Taxonomies.categories doesn’t work well.

2 Likes