Sorting categories

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

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.

I’m also interested in doing this, but unfortunately I don’t think it’s that simple. Check out this thread:

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!

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

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