How to sort pages of paginator?

I have a lot of pages that represent books. They have a taxonomy called “author” so I can list all the books by a specific author easily.

On the section page of books (/book/), I would like to list all the authors with the book titles, e.g.:
Author1 - Book1
Author1 - Book2
Author2 - Book3
Author3 - Book4

What I need is a sorted paginator (sorted by the author’s name and then by the book’s title). sort .Paginator.Pages "Params.authors" "asc" doesn’t seem to work, I have tried some other possibilities as well without success. How should one do this?

I’ve never used paginator before, but if it follows the same rules as ordering normal pages, give this a whirl:

{{ range .Paginator.Pages.ByParam "authors" }}
2 Likes

Thanks for the comment, but sadly the code does not work :frowning:
I tried to play around with different parameters but nothing seems to change, the “initial order” is kept (.Paginator.Pages).

You need to use .Paginate – as you need to sort them before you paginate them.

So

{{ range (.Paginate (.Pages.ByParam "authors")).Pages }}

Or variations of that.

2 Likes

Sorry, but still no success. :slight_smile: I am starting to think that it has something to do with the fact that authors is a taxonomy and the value of it is a list.

I suspect you need to show us the site source or something (GitHub link?) because it’s hard to guess.

Here you are @bep :

Note that authors -> szerzok because it is in Hungarian. (And I failed to make it “appear in links and html in Hungarian” but keep the variable names in English - Taxonomy terms page permalink - how to change it?)

@zwbetz @bep Could any of you guys kindly please take a look? This is a showstopper on my side which I cannot really fix on my own.

  1. The sort is on a section page that is a “list page”. The following layout is used:
    https://github.com/gabre/ppek-demo/blob/master/layouts/_default/list.html#L12
  2. This Hugo code is called, note ByParam and the “fallback” to string mode (by carrying out a string casting) if the selected parameter is not a number:
    https://github.com/gohugoio/hugo/blob/master/resources/page/pages_sort.go#L339
  3. String cast in case of a taxonomy that is a string slice results in an error (ToStringE in cast library) that is then swallowed in ToString (cast library, again).
    https://github.com/spf13/cast/blob/master/cast.go#L103

@zwbetz @bep may I kindly ask you to reply whether this is intended behaviour or am I wrong somewhere? What is the solution? Dropping usage of taxonomies in my case?

Am not sure, as I’m not too familiar with custom taxonomies, or paginators. Maybe someone else can help you.

Your .Params.szerzok is an array, which I suspect is why your sort does not work.

Try instead:

  1. Order szerzok taxonomy: https://gohugo.io/templates/taxonomy-templates/#taxonomy-methods
  2. Get pages asociated with taxonomy
  3. Paginate page list, which now has been presorted by szerzok from #1
{{ $pagslice := slice }}
{{ range (.Site.Taxonomies.szerzok.Alphabetical) }} {{/* #1 */}}
  {{range .Pages}} {{/* #2 */}}
    {{$pagslice = $pagslice | append .Page }}
  {{end}}
{{ end }}

{{ range (.Paginate $pagslice 1).Pages }} {{/* #3; .Paginate ... 1 to force pagination on small number of pages */}} 
  {{.Params.szerzok }}, {{ .Params.Title }}<br>
{{ end }}

{{ partial "pagination.html" . }}

1 Like