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!

I didn’t understand how you finally solved your issue. Did it hurt to post a brief summary on where an how you assigned your weights and what code did you put into the partial?

Hi @vistad, I’m sorry that you did not understand. Actually no, it did not hurt, I marked as solution what solved my problem and cleared my doubts. If you would like to see what was my final solution, you can ask for that, I will be happy to post it here for you.

Please post a sample how you achieved sorting range by assigning weights. Where did you put the weights, the syntax, the range code in the partial. Thank you!

Yes I am also interested @Korka13. I am concerned about having to organize my own tag cloud. If you can provide the example, it may be useful to others.

this is the code that I have in my sidebar partial
It loops through the taxonomies I created excluding the categories and lists them by weight

			{{ range $taxonomy_name, $taxonomy := .Site.Taxonomies }}
			{{ if (ne $taxonomy_name "categories") }}
			{{ with $.Site.GetPage (printf "/%s" $taxonomy_name) }}				
				<div class="list-group mb-5 d-none d-md-block">
					<h3 class="text-center text-capitalize list-group-item">{{ $taxonomy_name }}</h3>
					{{ with ($.Site.GetPage (printf "/%s" $taxonomy_name)) }}					
						{{ range .Pages.ByWeight }}
							<a class="list-group-item list-group-item-action text-capitalize" href="{{ .RelPermalink }}">{{ .Title }}</a>
						{{ end }}	
					{{ end }}
				</div>
			{{ end }}
			{{ end }}
			{{ end }}

this is printing out something like this:

for every taxonomy term that I wanted to be ordered by weight I had to create a file as follow (in this example “difficulty” is the taxonomy and “easy”, “medium” and “difficult” are the terms):

MyHugoSite\content\difficulty\easy\_index.md

---
title: "easy"
weight: 10
---

MyHugoSite\content\difficulty\medium\_index.md

---
title: "medium"
weight: 20
---

MyHugoSite\content\difficulty\difficult\_index.md

---
title: "difficult"
weight: 30
---

hope this helps!

Thank you!

Here’s a summary of @Korka13’s solution with the slightly simpler use case of needing to list terms in a custom order of only one taxonomy, in case it helps anyone doing this for the first time as I was.

My content is “projects”, with each project belonging to one “gallery”.

In /config.yaml:

taxonomies:
  gallery: galleries

Project content

/content/projects/soho/index.md:

---
title: "Soho"
galleries:
  - Kitchens
---

Taxonomy pages

/content/galleries/kitchens/index.md:

---
title: "Kitchens"
weight: 10
---

/content/galleries/commercial/index.md:

---
title: "Commercial"
weight: 20
---

Template

{{ $galleries := $.Site.GetPage "galleries" }}

<ul>
	{{ range $galleries.Pages.ByWeight -}}
		<li>
			<a href="{{ .RelPermalink }}" class="{{ .Title | urlize }}'">
				{{ .Title }}
			</a>
		</li>
	{{ end }}
</ul>

A post was split to a new topic: Taxonomy term question