Repository navigation
|
I'm trying to get my tags page sorted alphabeticaly (only this page). Tried lots of different things, but nothing works. The solution that seems like it should work, but it doesn't: /layouts/_default/list.html Any ideas? |
Replies: 1 comment 1 reply
|
The reason your solution doesn't work is due to a strict rule in Hugo: You can only call .Paginate once per template execution. In your current code, you are calling .Paginate unconditionally before your if statement. Once that first .Paginate runs, Hugo freezes the pagination data (using the default sort order). The second call inside your if $tagsPage block is either ignored or simply returns the already-cached, unsorted paginator. To fix this, you must determine the list of pages and their sort order before you initialize the paginator. Here is the corrected code logic: The Solution HTML $pages := .Pages: We capture the default page collection in a variable. $pages = $pages.ByTitle: If we are on the tags page, we modify that variable to use the .ByTitle sort method. This is efficient and succinct. Single .Paginate Call: We pass the final $pages variable to the paginator. This ensures the paginator is built exactly once with the correct order. |
The reason your solution doesn't work is due to a strict rule in Hugo: You can only call .Paginate once per template execution.
In your current code, you are calling .Paginate unconditionally before your if statement. Once that first .Paginate runs, Hugo freezes the pagination data (using the default sort order). The second call inside your if $tagsPage block is either ignored or simply returns the already-cached, unsorted paginator.
To fix this, you must determine the list of pages and their sort order before you initialize the paginator.
Here is the corrected code logic:
The Solution
You need to create a variable for your pages (e.g., $p), sort that variable if necessary, and then pass …