Display all content related to taxonomy - with {{.title}}

I have modified the template from the docs to suit my needs, but I’m having an issue with the following snippet:

{{ range $key, $taxonomy := .Site.Taxonomies.state }}
{{ $key }}
    {{ range $taxonomy.Pages }}
    <li hugo-nav="{{ .RelPermalink}}"><a href="{{ .Permalink}}"> {{ .LinkTitle }} </a> </li>
    {{ end }}
{{ end }}

Instead of $key I would like it to display {{ .Title }} from the relevant taxonomy, but if I change the code for that I says:

executing “main” at <.Title>: can’t evaluate field Title in type hugolib.WeightedPages

What does $key do, and how can I change it to output {{.Title}} instead?

I’m rusty on taxonomies, but I’m assuming it would be a property of $taxonomy. Try these:

{{ $taxonomy.Title }}
{{ $taxonomy.Name }}

You don’t have anything else but the terms’ pages in there. So no .Title or else available, just your $key and your Pages.

But the Key should display the name of the term doesn’t it ?

Also when you say taxonomy, you mean term of the taxonomy state right ? Like Dakota would be a term, State being the taxonomy.

Yeah no $key is the slug…

Here’s what you can do from your range:

{{ $term := $.Site.GetPage "taxonomy" "states" $key }}
{{ $term.Title }}

If it’s not “states” try “state”.

1 Like

So, in my example, where does this code go?

Where do you want to get the title? Do you have it in /state/_index.md?

From /state/“state_name”/_index.md

From within the range which lists states (the first one) as I mentioned above. First line grabs the term information, second one displays.

It will work even if you don’t have an _index.md

Thank you! Works like a charm.