[SOLVED] For a given .RelatedTo index name, list all pages per the value of that index?

Given a website with :

  • only one section named “post” in content/
  • one related index named “category”

This is how I implemented it, I want to do some kind of reverse indexing: retrieve all existing values of that index, make it a unique list, iterate through that list and get all matching pages using RelatedTo.

Could it be done differently and faster? At least without having to use.Scatch?

{{range .Site.Pages }}
  {{ $.Scratch.Add "categories" (slice .Params.category) }}
{{end}}
...
{{ range (uniq ($.Scratch.Get "categories")) }}
<h5>{{ . }}</h5>
  {{ range $.Site.RegularPages.RelatedTo ( keyVals "category" . ) }}
    <a href="{{ .URL }}">{{ .Title }}</a>
  {{ end }}
{{ end }}

I’m trying to hack Hugo into some sort of wiki-style website :smiley:

Hey there,

from what I understand, you want to lists pages grouped by categories.

You don’t need related for that.

You have what you need stored in .Site.Taxonomies.categories. Reading this, you can use the following with confidence:

{{ range $key, $value := .Site.Taxonomies.categories }}
  <h5>{{ $key }}</h5>
  {{ range $value.Pages }}
    <a href="{{ .URL }}">{{ .Title }}</a>
  {{ end }}
{{ end }}
2 Likes

This is exactly what I needed, thank you @regis !

That is indeed a really simple way. I’ve been using Hugo for a while but that probably means I didn’t understand what taxonomies were for until now!

2 Likes