teddy
October 19, 2019, 3:39pm
1
Hi,
I have three categories in my taxonomy: “Html”, “Css” and “Tools”.
In my menu partial, I list them like this:
{{ range $key, $value := .Site.Taxonomies.kategorier }}
<h4>{{ $key }}</h4>
<ul>
{{ range $value.Pages }}
<li>
<a href="{{ .Permalink}}">{{ .LinkTitle }}</a>
</li>
{{ end }}
</ul>
{{ end }}
Is there any way to sort the categories in a chosen order? I mean, not alphabetically or by number of posts, but by my own priority?
1 Like
teddy
October 19, 2019, 7:42pm
3
Thank you. I read that page, but couldn’t find an answer.
The way I’m reading the docs, they say how you can weight different taxonomies against each other, but not how you can sort the listings of one specific taxonomy (in my case “kategorier”).
It’s probably a simple answer, but I think I still need help, I’m afraid.
_nm
October 19, 2019, 9:19pm
4
I’m also interested in doing this, but unfortunately I don’t think it’s that simple. Check out this thread:
Update: Full code at Is it possible to sort taxonomy terms based on a set term weight?
The following code loops through taxonomy categories and prints links to the posts sorted by category.
How could I add weights to taxonomies?
How could I adapt the following code so to be able to sort categories based on given weights?
Thanks
{{ range $key, $taxonomy := .Site.Taxonomies.categories }}
<h2>{{ $key }}</h2>
<ul>
{{ range $taxonomy.Pages }}
<li><a href="{…
And threads listed in this search:
https://discourse.gohugo.io/search?q=sort%20terms
If anyone knows a simple way to do this, please post!
ju52
October 20, 2019, 7:06am
5
To sort all terms of a category / taxonomay you can use sort:
{{ range $index, $name := sort site.Taxonomies.tags.Alphabetical }}
for sort look at the doc
If you need an custom sort, you must create an arry / map and ADD the elements in the right order, then you can range over it.
look at .add and .SetinMap and .GetSortedMapValues
teddy
October 20, 2019, 8:09am
6
Thanks again for helping, jr52. I ended up solving it in a slightly different way.
In my site params I added
kategori_order = ["tools","html","css"]
and in my menu template, I wrote:
{{ $kategorier := .Site.Taxonomies.kategorier }}
{{ range $kat := .Site.Params.kategori_order }}
{{ $value := index $kategorier $kat }}
<h4>{{ $kat | humanize }}</h4>
<ul>
{{ range $value.Pages }}
<li>
<a href="{{ .Permalink}}">{{ .LinkTitle }}</a>
</li>
{{ end }}
</ul>
{{ end }}
2 Likes