[SOLVED] Dynamically filter pages by taxonomy

I tried $brand = "Nike" but of course I then realized that it shouldn’t be a string. It should be a taxonomy object. I also got confused trying to do that. I guess I should have ranged trough taxonomies and then scratched the taxonomy that matches… right? Something like that… I’ll have to play with that.

Anyway, I managed to find a solution but it is horrible, and I’m sure there should be an easier method. I mean, listing all the pages under the same taxonomy is something very common. So if somebody has a nicer solution, please show me the light!

Here’s what I did:

{{ range .Site.Taxonomies }}
  {{ range $key, $value := . }}
    {{ if eq (urlize $key) (urlize (lower $.Title)) }}
      {{ range $value.Pages }}
        {{ .Title }}{{ .Params.logo }}
      {{ end }}
    {{ end }}
  {{ end }}
{{ end }}

I’m comparing the page title against the taxonomy term. {{ if eq (urlize $key) (urlize (lower $.Title)) }} I needed to urlize and lower them because some had latin characters.

As I said, it works, but it’s ugly. I’m pretty sure that it isn’t very efficient either. Luckily hugo is lightning fast, so I don’t really care if the build takes 0.5s longer.

2 Likes