Unable to find sections with taxonomy

Hello,

I have this frontmatter in a _index.md file:

+++
title = "My section"
date = 2020-11-03T14:36:45+01:00
weight = 100
chapter = false
categories = ["mycat"]
+++

But I cannot retrieve it when I use this where query:

      {{- range $cat, $taxo := .Site.Taxonomies.categories }}
      {{ $cat }}
      {{ $taxo }}
      {{ $p := slice $cat }}
      {{ range  where $.Site.Pages  "Params.categories" "intersect" $p }}
      {{ .Page.Kind }}
      {{ .Page.Params.categories}}
      {{end}}
      {{end }}  

I don’t know what I am missing. If someone can point me in the good direction it would be appreciated :slight_smile:

Regards,

Alain

categories is not an single value, use range!

{{ range (.Page.GetTerms “categories”) }} should work

Hello,

Thanks but I am not sure how to put this range on categories in my where statement:

{{ range  where $.Site.Pages  "Params.categories" "intersect" $p }}

Is it possible ? I thought intersect was doing something similar

replacement for {{ .Page.Params.categories}}

	{{ range sort (.Page.GetTerms "categories") "LinkTitle" -}}
		<a  href={{ .RelPermalink }} >{{ .LinkTitle }}</a>
	{{- end}}

only a fast try :slight_smile:

1 Like

Hello,

Yes but I would like to filter pages by a category value.

It is unclear to me what you are trying to accomplish. If you wish to display all pages with a term in the “categories” taxonomy:

{{ range $term, $weightedPages := .Site.Taxonomies.categories }}
  <h2>{{ $term }}</h2>
  {{ range $weightedPages }}
    <a href="{{ .RelPermalink }}">{{ .Title }}</a>
  {{ end }}
{{ end }}

Hello,

Thanks.

Sorry I will try to explain myself in a better way.

In your example, I will get all pages.

I would like to filter them by category.

So if I have this frontmatter for myfile.md:

+++
title = "My section"
date = 2020-11-03T14:36:45+01:00
categories = ["mycat"]
+++

And this frontmatter for the myfile2.md:

+++
title = "My section"
date = 2020-11-03T14:36:45+01:00
categories = ["myothercat"]
+++

I want to filter pages to retrieve only the page where the value in cateogires is mycat.

I tried this code but it’s not working:

{{- range $cat, $taxo := .Site.Taxonomies.categories }}
      <h1>{{ $cat }}</h1>
      {{ $taxo }}
      {{ $p := slice $cat }}
      {{ range  where $.Site.Pages  "Params.categories" "intersect" $p }}
      {{ .Page.Kind }}
      {{ .Page.Params.categories}}
      {{end}}
      {{end }}
{{ range where site.Pages "Params.categories" "intersect" (slice "mycat") }}
  <h2><a href="{{ .RelPermalink }}">{{ .Title }}</a></h2>
{{ end }}
1 Like

Thanks :slight_smile:

It’s working with this query:

{{ range  where (where site.Pages "Kind" "section")  "Params.categories" "intersect" (slice $cat) }}

I don’t understand what is the difference between $.Site.Pages and site.Pages. it seems to be the only difference between our code.

https://gohugo.io/functions/site/

1 Like

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