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
<ul>
{{ range .Pages }}
<li><a href="{{ .Page.RelPermalink }}">{{ .Page.Title }}</a></li>
{{ end }}
</ul>
When I try to filter for a specific taxonomy (category=car), it gathers all the cars from the entire website.
<ul>
{{ range .Site.Taxonomies.categories.cars}}
<li><a href="{{ .Page.RelPermalink }}">{{ .Page.Title }}</a></li>
{{ end }}
</ul>
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 .Pages.Taxonomies.categories.cars}} 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.
{{- $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>
<p>Categories:
{{- range (.GetTerms "categories") }}
<a href="{{ .Permalink }}">{{ .LinkTitle }}</a>
{{- end }}
</p>
<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 https://github.com/jmooring/hugo-testing hugo-forum-topic-26882
cd hugo-forum-topic-26882
hugo server
Thank you so much for the solution. It works as expected. . 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?