Is it possible to sort taxonomy terms based on a set term weight?

use .WeightedPages ?

I don’t understand what this is and how to use it.

I have /content/categories/term/_index.md.
At frontmatter I have a weight param.

I don’t understand how to use it to sort categories.

Does this ever happened before?

       {{ range $taxonomy.WeightedPages}}
           <li><a href="{{ .RelPermalink }}">{{ .Title }}</a></li>
      {{ end }}

this way…

But I want to sort taxonomies, NOT pages inside them. I can sort pages inside taxonomies using ByWeight.
Also adding .WeightedPages after categories does not work
Thanks :slight_smile:

{{ range $key, $taxonomy := .Site.Taxonomies.categories.WeightedPages }}
   {{ range .Pages.ByWeight }}
   {{ end }}
{{ end }}

I’m not sure it works, but I’d try assigning weight to the front matter of the taxonomy terms. If the built-in weight mechanism doesn’t work for terms, then you can always code it up in your partial. :slight_smile:

Yes, I have assigned weights on the frontmatter of taxonomy term pages.

I have /content/categories/term/_index.md where I include weight in frontmatter.

The matter is that I don’t know how to retrieve this info and include at the aforementioned code.

Although I am a developer, I am not any kind of advance go lang developer. I have not found anywhere such an example.

Can this be done or I am searching in vain?

You are searching in vain because you aren’t sharing your code/repo. Without seeing what you are doing, we are guessing. If you can’t share your site’s repo, please make a new one you can, and reproduce your issue. But once we can see what’s happening, someone will be able to tell you one way or another.

3 Likes

The repo of simple code that shows my issue is at

My issue is how to rewrite layouts/index.html so to order taxonomies based on weights set at /content/categories/term/index.md frontmatter.

At home page I get now:
extralarge
item5
item6

great
item1
item2

large
item3
item4

What I would like to get is:
great
item1
item2

large
item3
item4

extralarge
item5
item6

I hope now it’s clear.

Thanks

1 Like

In my first reply - look at the linked page.
There is a paramter in Frontmatter

categories_weight = 44

Don’t know where to apply it, in the item files or in the category/index files.

must try it out.

@ju52 Thanks for your reply
I added categories_weight: 2 at the frontmatter.
Nothing changes and I suppose nothing will change until I find how to retrieve this data to sort
at
{{ range $key, $taxonomy := .Site.Taxonomies.categories }}

@ju52 categories_weight = 44 is for weighing a piece of content inside a taxonomy (categories in this case). The poster wants to weigh terms against each other.

1 Like

If I try
{{ range sort $key, $taxonomy := .Site.Taxonomies.categories “weight” }}
then I get error
parse failed: template:: undefined variable “$key”

If
{{ range $key, $taxonomy := (sort .Site.Taxonomies.categories “.Params.weight”) }}
then
error calling sort: Params is neither a struct field, a method nor a map element of type page.WeightedPages

How can I access the weights of the taxonomy terms?

Hello again @johnson

The solution to your issue lies in this post by @pointyfar over here: List custom taxonomy terms in a specific order (not alphabetical)

Simply adapt the linked code in /layouts/index.html of your sample repo like so:

{{ define "main"}}
{{ $taxo := "categories" }}
<ul>
  {{ with ($.Site.GetPage (printf "/%s" $taxo)) }}
    {{ range .Pages.ByWeight }}
      <li><a href="{{ .RelPermalink }}">{{ .File.Dir | title }}</a></li>
    {{ end }}
  {{ end }}
</ul>
{{ end }}

Once you execute hugo server you will be greeted with the following list:

   • Categories/Great/
   • Categories/Large/
   • Categories/Extralarge/
1 Like

Thanks @alexandros for your answer.

So, isn’t it possible to have .Site.Taxonomies.categories.ByWeight?
Is there a way to have .Site.Taxonomies.categories.ByBaparam “weight” ?

Should we handle categories as pages with .GetPage to manage to sort them by weight?

On your example, how I can go over the posts under each catagory?
The output of your example is not what I am trying to achieve.

What I would like to get is:
great
item1
item2

large
item3
item4

extralarge
item5
item6

Thanks!

The answer to your question is in the post I linked to.

I did not copy that code block in its entirety because I thought you wanted a sorted taxonomy terms list, (I hadn’t read every post above).

In any case it’s certainly doable to render the list as you ask, like this:

{{ define "main"}}
{{ $taxo := "categories" }}
<ul>
  {{ with ($.Site.GetPage (printf "/%s" $taxo)) }}
    {{ range .Pages.ByWeight }}
      <li><a href="{{ .RelPermalink }}">{{ .File.Dir | 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>
{{ end }}

Screenshot of end result:

sorted-taxa

4 Likes

Thanks a lot. This starts to look almost as I m trying to

To achieve the wanted output I have to change
Categories/Great (as in your example)
to just
category (that is taxonomy term).

So, how can I retrieve the category term? (great, large etc)

Replace {{ .File.Dir | title }} with {{ .Title }} but you need to specify the titles in the front matter of the taxonomy terms’ _index.md (currently in these files you have only entered weight and as a result the .Title variable returns empty, I used the .File.Dir variable to illustrate the end result.

Also have a look at: Taxonomies | Hugo

3 Likes

Alternatively, we may use {{ path.Base .File.Dir }}.

So, essentially we treat taxonomy as a page.

Essentially, as I understand what’s missing are
.Site.Taxonomies.categories.ByWeight and .ByParam
Missing those we have to use more complicated solutions with GetPage and treat taxonomy as a page.
We cannot retrieve taxonomy params outside taxonomy templates. Right?

@alexandros Thanks a lot for your help. I really appreciate.

1 Like

@alexandros
Unfortunately this solutions does not work if I have
disableKinds: [“taxonomy”,“taxonomyTerm”] at my config.yaml

Since taxonomy pages are not wished in the site, I have to use this solution and then with a script delete the taxonomy pages. So, this is not an ideal situation.