Difficulties with terms and taxonomies in specific cases

I searched the official documentation (which is constantly being improved) and the forum. But I haven’t found very clear answers to my questions.

Today, I feel like an idiot who doesn’t understand anything about Hugo.

I have 2 taxonomies declared in the configuration:

[taxonomies]
geo = "geo" # no plural, it's a choice
theme = "themes"

And in my pages:

geo = ["france", "germany", "switzerland"]
themes = ["cinema", "hugo", "music"]

I’m looking to do 3 separate things that are causing me problems.

On a content page (kind = page)

I’d like to list taxonomies and terms, but not do things like that:

{{ $taxonomyObject := .Site.Taxonomies.geo }}

<p>Geo: {{ range $taxonomyObject}} ... {{ end }}</p>

I’d like the taxonomy to be displayed if and only if it’s used on the page, then list the terms. In pseudo code:

range pageTaxonomies:
    print TaxonomyTitle
    
    range Terms:
        print Term

So when I’m on a paris page, I’d like to see:

geo:
- france

themes:
- cinema
- music

On a taxonomy page (kind = taxonomy)

I’d like to list the site’s other taxonomies as related.

For the moment, I’m listing the terms in the taxonomy, classic usage.

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

Below, I’d like a “see also”: list of other taxonomies on the site.

So when I’m on geo, I’d like to see:

geo:
- france
- germany
- swizerland

see also:
- themes

I think that depending on the variables that exist in Hugo, I need to go through site.Taxonomies and remove the current one with an if eq.... Right? Or is there another way of doing this?

I have an answer:

<ul>
  {{ range (where site.AllPages "Kind" "taxonomy")  }}
    {{ if ne .Page page }} 
      <li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
    {{ end }}
  {{ end}}
</ul>

On a term page (type = term)

Below the list (similar to the code above), I’d like to see the other terms.

So when I’m on france, I’d like to see:

france:
- marseille 
- paris
- [my pages]

see also:
- germany
- switzerland

It’s for this part of see also that I have no idea how to go about it. I know I missed something, but I’m still wondering what it is…

I have an answer (taxonomy is a parent of terms!):

{{ with .Parent }}
<ul>
  {{ range .Pages }}
    {{ if ne .Page page }} 
      <li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
    {{ end }}
  {{ end}}
</ul>
{{ end }}

Thank you in advance for giving me some clues to help me feel less stupid.

For the first one, use the GetTerms method on the Page object.

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

A couple of notes…

  • Filter out empty taxonomies
  • Be careful how you use the global page function (see warnings)
1 Like

Thank you so much! You are always so helpful and precise. Impressive and helpful.

I will read your code carefully and learn a lot.

1 Like

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