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
Thank you!!
EDIT:
As a workaround I’m defining my taxonomy terms as:
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/
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
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.
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!
{{ $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>
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.
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):
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”.