Sort .Pages by Weight of Params (Taxonomy)

Hi,

I would like to sort .Pages by Weight of Params (Taxonomy).

I have Section “persons” with Taxonomy “status”, like:

---
title: John Doe
status: President
---
---
title: Bruce Doe
status: Minister
---
---
title: Ali Doe
status: Minister
---

I have a Taxonomy “status” with some term with weight

---
title: President
weight: 1
---
---
title: Minister
weight: 2
---

So, in “persons” list I have a .Paginator.Pages like:

.Paginator.Pages.ByParam "status" 

Expected result (John Doe in first because his status is President then ministers):

  1. John Doe
  2. Ali Doe
  3. Bruce Doe

But this is sort only by Alphabetical not by Weight of Params

Any idea?

Thanks a lot.
Seb

This does what you want. If a person does not have a “status”, or if the “status” does not have a non-zero weight, that person will sink to the bottom of the list.

If the person front matter has two or more status values, we only consider the first. For example:

+++
title = 'Richard Doe'
status = ['mayor','ignored']
+++
layouts/persons/list.html
{{/* Create slice of maps, where each map contains the page and term weight. */}}
{{ $taxonomy := "status" }}
{{ $s := slice }}
{{ range .Pages }}
  {{ $termWeight := 99999999 }}
  {{ with (index (.GetTerms $taxonomy) 0).Weight }}
    {{ $termWeight = . }}
  {{ end }}
  {{ $s = $s | append (dict
      "page" .
      "termWeight" $termWeight
    )
  }}
{{ end }}

{{/* Create a slice of pages, sorted by the term weight. */}}
{{ $p := slice }}
{{ range (sort $s "termWeight") }}
  {{ $p = $p | append .page }}
{{ end }}

{{/* Render the page collection. */}}
{{ range (.Paginate $p).Pages }}
  <h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a> ({{ index .Params.status 0 }})</h2>
{{ end }}

For example:

git clone --single-branch -b hugo-forum-topic-44820 https://github.com/jmooring/hugo-testing hugo-forum-topic-44820
cd hugo-forum-topic-44820
hugo server

Thanks a lot @jmooring, it’s perfect!

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.