1
0
Fork 0
hugo/docs/content/en/methods/pages/_common/next-and-prev.md

1.8 KiB

Hugo determines the next and previous page by sorting the page collection according to this sorting hierarchy:

Field Precedence Sort direction
weight 1 descending
date 2 descending
linkTitle 3 descending
path 4 descending

The sorted page collection used to determine the next and previous page is independent of other page collections, which may lead to unexpected behavior.

For example, with this content structure:

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

And these templates:

{{< code file=layouts/_default/list.html >}} {{ range .Pages.ByWeight}}

{{ .LinkTitle }}

{{ end }} {{< /code >}}

{{< code file=layouts/_default/single.html >}} {{ $pages := .CurrentSection.Pages.ByWeight }}

{{ with $pages.Prev . }} Previous {{ end }}

{{ with $pages.Next . }} Next {{ end }} {{< /code >}}

When you visit page-2:

  • The Prev method points to page-3
  • The Next method points to page-1

To reverse the meaning of next and previous you can chain the Reverse method to the page collection definition:

{{< code file=layouts/_default/single.html >}} {{ $pages := .CurrentSection.Pages.ByWeight.Reverse }}

{{ with $pages.Prev . }} Previous {{ end }}

{{ with $pages.Next . }} Next {{ end }} {{< /code >}}