Cyclic navigation within a section (verification question)

For a personal project, I’d like a cyclical navigation (webring style) in a section.

Everything works fine with .Prev and .Next. But I’d like to add the 1st and last page when you’re at the end/beginning of the list.

My code, which works fine (there are no conditions because there are always pages in this section):

{{ $pages := .CurrentSection.Pages.ByLinkTitle.Reverse }}
{{ $prev := or ($pages.Prev .) (index (first 1 $pages) 0) }}
{{ $next := or ($pages.Next .) (index (last 1 $pages) 0) }}

<div class="webring">
  <a href="{{ $prev.RelPermalink }}" title="{{ $prev.LinkTitle }}">Précédent</a>
  <a href="{{ $next.RelPermalink }}" title="{{ $next.LinkTitle }}">Suivant</a>
</div>

My question is quite simple. Is it possible to list the 1st and last page without index? I find my code rather inelegant.

And by the way, I saw that sorting could be by date (.ByDate), weight (.ByWeight), title (.ByLinkTitre), but not by path. Right? I’m talking about the default filters.

No, but you don’t have to use first and last.

index (first 1 $pages) 0 --> index $pages 0
index (last 1 $pages) 0 --> index $pages (sub $pages.Len 1)

These are the available page collection sorting methods:
https://gohugo.io/quick-reference/page-collections/#sort

The is not a ByPath method.

1 Like

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