Next
Syntax
PAGES.Next PAGE
Returns
page.Page
The behavior of the Prev
and Next
methods on a Pages
objects is probably the reverse of what you expect.
With this content structure and the page collection sorted by weight in ascending order:
content/
├── pages/
│ ├── _index.md
│ ├── page-1.md <-- front matter: weight = 10
│ ├── page-2.md <-- front matter: weight = 20
│ └── page-3.md <-- front matter: weight = 30
└── _index.md
When you visit page-2:
- The
Prev
method points to page-3 - The
Next
method points to page-1
{{ $pages := where .Site.RegularPages.ByWeight "Section" "pages" }}
{{ with $pages.Next . }}
<a href="{{ .RelPermalink }}">Previous</a>
{{ end }}
{{ with $pages.Prev . }}
<a href="{{ .RelPermalink }}">Next</a>
{{ end }}
Compare to Page methods
The Next
and Prev
methods on a Pages
object are more flexible than the Next
and Prev
methods on a Page
object.
Page collection | Custom sort order | |
---|---|---|
PAGES.Next and PAGES.Prev |
locally defined | ✔️ |
PAGE.Next and PAGE.Prev |
globally defined | ❌ |
- locally defined
- Build the page collection every time you call
PAGES.Next
andPAGES.Prev
. Navigation between pages is relative to the current page’s position within the local collection, independent of the global collection.
With a local collection, the navigation sort order is the same as the collection sort order.
- globally defined
- Build the page collection once, on a list page. Navigation between pages is relative to the current page’s position within the global collection.
With a global collection, the navigation sort order is fixed, using Hugo’s default sort order. In order of precedence:
- Page weight
- Page date (descending)
- Page linkTitle, falling back to page title
- Page file path if the page is backed by a file
For example, with a global collection sorted by title, the navigation sort order will use Hugo’s default sort order. This is probably not what you want or expect. For this reason, the Next
and Prev
methods on a Pages
object are generally a better choice.