Hugo

List custom taxonomy terms in a specific order (not alphabetical)

Hello,

I’m building a hugo theme with some custom taxonomies.
The intention is to build it to be compatible with Netlify CMS.
In a sidebar I want to list the terms of one taxonomy with a link to the show page of that specific term.
What I want to achieve is to list those terms in a specific order, not alphabetical.
The reason for that is that if the website is for example talking about food recipes, a taxonomy would be “Difficulty” - and the terms for example “Easy, Medium, Difficult, Very Difficult”, and would be good to list them in the sidebar in this order, and not alphabetical.

A nice resolution would be to be able to define all the taxonomy terms already in the config file with a weight

Unfortunately I could not find any solution in the docs or other answers, if it was due to lack of understanding from my side please excuse me but I don’t know how else proceed :slight_smile:

Thank you!!

EDIT:
As a workaround I’m defining my taxonomy terms as:

01 Easy
02 Medium
03 Difficult
04 Very Difficult

and in the sidebar template:

				<h3>Difficulty</h3>
				<ul>
					{{ range $name, $taxonomy := .Site.Taxonomies.difficulty }}
					<li><a href="{{ "/difficulty/" | relLangURL }}{{ $name | urlize | lower }}">{{ trim $name "0123456789" | humanize }}</a></li>
					{{ end }}
				</ul>

This is ordering the terms in the order I wish and is not displaying the numbers, of course if I had numbers in my terms it wouldn’t be a solution.

My concern at this point is that the slug of the taxonomy term is /difficulty/01-easy/ and I don’t want that. How can I tell Hugo to create the list pages in a different slug? for instance /difficulty/easy/

Thank you!!!

Hi,

You should be able to assign a weight in the front matter of your term, and then keep the terms easy medium etc.

Hello @pointyfar and thanks a lot for your response.
Is it possible to define weight and other properties for a taxonomy term in the config file?
I do not want to create for every taxonomy term an .md file and a list.html template.
Something like

[[taxonomy.difficulty]]
  name = "easy"
  weight = "10"

would be amazing, is there the possibility to do similar declarations?

or else with

[permalinks]
  difficulty = "/:section/:slug/"

is there a way to loop through the :slugs and remove particular characters?

Or even something like

[permalinks]
  difficulty.01-easy = "/easy/"

I would like to keep the theme to use only its html files and the config.toml, without relying in single .md files for all the taxonomies to be defined

Thanks a lot!!

I don’t think those are currently supported.

You could define the permalink configuration, but you would still need to specify the slug and/or title in the term’s front matter, if you want it to use custom values.

Hi @pointyfar, thanks a lot for keeping an eye on my issue.
I tried to define all taxonomy terms with a weight in the respective front matter:
(i.e.):

/content/difficulty/easy/_index.md
---
title: "easy"
weight: 10
---
/content/difficulty/medium/_index.md
---
title: "medium"
weight: 20
---

etc etc…
In my sidebar partial template I’m listing the taxonomy like this:
{{ range $name, $taxonomy := .Site.Taxonomies.difficulty }}
<a href="{{ “/difficulty/” | relLangURL }}{{ $name | urlize | lower }}">{{ $name | humanize }}
{{ end }}

I’ve tried also adding .ByWeight but I’m not able to list them in the right order.
I’m probably missing something… I’m using Hugo version 0.55
Thanks for any suggestion!

We need to do that slightly differently, based on this example: https://gohugo.io/templates/taxonomy-templates/#site-getpage-for-taxonomies

{{ $taxo := "difficulty" }}
<ul>
  {{ with ($.Site.GetPage (printf "/%s" $taxo)) }}
    {{ range .Pages.ByWeight }}
      <li><a href="{{ .Permalink }}">{{ .Title}}</a>
        <ul>
          {{ range .Pages }}
          <!-- this also lists the content pages associated with the term; 
               remove if not needed. -->
          <li>{{ .Title }}</li>
          {{ end }}
        </ul>
      </li>
    {{ end }}
  {{ end }}
</ul>

It works perfectly.
Thanks!