Prev

Hugo 透過依照以下排序層級對頁面集合進行排序,來確定 下一頁上一頁

欄位 優先權 排序方向
weight 1 降冪
date 2 降冪
linkTitle 3 降冪
path 4 降冪

用來確定 下一頁上一頁 的排序後頁面集合是獨立於其他頁面集合的,這可能會導致預期外的行為。

例如,以下是內容結構:

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

以及這些範本:

layouts/_default/list.html
{{ range .Pages.ByWeight}}
  <h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
{{ end }}
layouts/_default/single.html
{{ $pages := .CurrentSection.Pages.ByWeight }}

{{ with $pages.Prev . }}
  <a href="{{ .RelPermalink }}">上一頁</a>
{{ end }}

{{ with $pages.Next . }}
  <a href="{{ .RelPermalink }}">下一頁</a>
{{ end }}

當你訪問 page-2 時:

  • Prev 方法指向 page-3
  • Next 方法指向 page-1

若要反轉 下一頁上一頁 的意義,可以將 Reverse 方法鏈接到頁面集合定義中:

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

{{ with $pages.Prev . }}
  <a href="{{ .RelPermalink }}">上一頁</a>
{{ end }}

{{ with $pages.Next . }}
  <a href="{{ .RelPermalink }}">下一頁</a>
{{ end }}