Can .GetTerms support an array of taxonomies?

I am curious coz I am looking for a way to define via .Param which terms can show up and a default term to fall back on. For example, I would like to do get_terms = ['tags'] or get_terms = ['tags','categories'] and then do .GetTerms ($.Param "get_terms").

I am also open to more ideas to configure such a thing.

You would need an outer loop to range through the taxonomies, and an inner loop to range through the terms returns by .GetTerms, discarding those you don’t want.

An example in code would be appreciated.

Something like…

git clone --single-branch -b hugo-forum-topic-56095 https://github.com/jmooring/hugo-testing hugo-forum-topic-56095
cd hugo-forum-topic-56095
hugo server

Files of interest:

  • hugo.toml
  • layouts/_partials/terms.html

The partial does two things:

  1. Builds a data structure of the terms assigned to the current page, grouped by taxonomy, excluding terms in the “exclude list” as defined in front matter (if any), falling back to the “exclude list” as defined in the site configuration (if any). Invert the logic if you’d rather use an “include list”.
  2. Renders the data structure as a nested, unordered list.

Default terms are assigned by cascading from the site configuration.

Hi Joe. I made a mistake when I said “terms”. What I really meant is this.

./config/default/params.toml

[list]
  get_ terms = [''] # e.g ['tags'], ['categories','tags'] etc. Could be any user defined taxonomy

[page]
  get_ terms = [''] # e.g ['tags'], ['categories','tags'] etc. Could be any user defined taxonomy

So, when a taxonomy is added (or taxonomies are added) in such a way, then the terms from those taxonomies will show up. e.g

./layout/list.html

{{ with $.Param "list.get_terms" | default "categories" }}
  <!--- Code for .GetTerms goes here. Preferably a reusable partial--->
{{ end }}

./layout/page.html

{{ with $.Param "page.get_terms" | default "categories"}}
  <!--- Code for .GetTerms goes here. Preferably a reusable partial--->
{{ end }}

The reusable partial will then fetch the given terms for the defined taxonomies (array) and fall back to categories taxonomy terms if none are user defined.

Param here means it should be configurable per page or in params.

The use case is showing the defined taxonomy’s terms in each single page or in each page entry in the list page.

(Maybe get_terms should be get_taxonomy_terms.)

I hope that’s clear. Thanks in advance.

(The configuration is defined like [list] and [page] also coz styling is different, alongside configuring appearace for page and list pages.)

The approach is the same: outer loop and inner loop.

Thanks for the help. Grok filled in the blanks

 {{ $getTerms := $.Param "taxonomies.get_terms" | default slice }}
{{ $allTaxonomyData := slice }}
{{ range $taxonomyName, $taxonomy := site.Taxonomies }}
  {{ if in $getTerms $taxonomyName }}
    {{ $visibleTerms := slice }}
    {{ range $.GetTerms $taxonomyName }}
      {{ $visibleTerms = $visibleTerms | append . }}
    {{ end }}
    {{ with $visibleTerms }}
      {{ $allTaxonomyData = $allTaxonomyData | append (dict "termPages" $visibleTerms) }}
    {{ end }}
  {{ end }}
{{ end }}

{{ with $allTaxonomyData }}
  <ul>
    {{ range . }}
      {{ with .termPages }}
        {{ range . }}
          <li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
        {{ end }}
      {{ end }}
    {{ end }}
  </ul>
{{ end }}

Now I just change this taxonomies.get_terms to list.taxonomies.get_terms or pagetaxonomies.get_terms for list or single pages.

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