Taxonomy filters based on Sections


I am trying to create a taxonomy filter based on separate Content Sections.
Basically, I have separate folders as separate sections that Hugo reads.

  • When I try this from within a folder, I am able to get ONLY the pages within that folder(section), but all the pages show up irrespective of the category type

      {{ range .Pages }}
          <li><a href="{{ .Page.RelPermalink }}">{{ .Page.Title }}</a></li>
      {{ end }}
  • When I try to filter for a specific taxonomy (category=car), it gathers all the cars from the entire website.

      {{ range}}
          <li><a href="{{ .Page.RelPermalink }}">{{ .Page.Title }}</a></li>
      {{ end }}

What I am looking for is something where I can filter for cars(category) from within a particular folder(section), instead of the entire site.

Trying {{ range}} fails and I don’t know how to get it working.

Please help. I am new to Hugo and it would be helpful if you could give the code directly. Thanks.

Here’s an example using a shortcode:

{{< filter section="movie" category="cars" >}}


{{- $section := "" -}}
{{- $category := "" -}}

{{- with .Get "section" -}}
  {{- $section = . -}}
{{- else -}}
  {{- errorf "The '%s' shortcode requires a named parameter: section. See %s" .Name .Position -}}
{{- end -}}

{{- with .Get "category" -}}
  {{- $category = . -}}
{{- else -}}
  {{- errorf "The '%s' shortcode requires a named parameter: category. See %s" .Name .Position -}}
{{- end -}}

{{- $pages := where (index .Site.Taxonomies.categories $category).Pages "Section" $section -}}

<h2>Pages in section "{{ $section }}" with category "{{ $category }}"</h2>

{{- range $pages }}
  <h3><a href="{{ .RelPermalink }}">{{ .Title }}</a></h3>
    {{- range (.GetTerms "categories") }}
    <a href="{{ .Permalink }}">{{ .LinkTitle }}</a>
    {{- end }}
  <p>{{ .Summary }}</p>
  {{- if .Truncated }}
  <p><a href="{{ .RelPermalink }}">Continue reading...</a></p>
  {{- end -}}
{{- end -}}

Try it:

git clone --single-branch -b hugo-forum-topic-26882 hugo-forum-topic-26882
cd hugo-forum-topic-26882
hugo server

Thank you so much for the solution. It works as expected. :star_struck:. Appreciate the help.

However this is the markdown+shortcode approach and I was looking to a HTML version. Could this be translated to work in the section.html which fetches the section automatically and uses hard-coded category values?

Yes, it can.

@jmooring Got the html version working. :slight_smile:

{{- range $pages := where (index .Site.Taxonomies.categories "cars").Pages "Section" "movie" -}}

Is there a way to automatically let Hugo get the section it is in? Eg ‘movie’ ‘article’ ‘post’ without specifying manually?

I can specify the category manually, just the section part is eluding me. Please help, if possible. Thanks

Two options:

{{- range where (index .Site.Taxonomies.categories "cars").Pages "Section" .Section -}}


{{- range where .RegularPages "Params.categories" "intersect" (slice "cars") -}}
1 Like

Thank you so much for you help. Everything works perfectly now :star2:

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.