PrevInSection
返回当前栏目中相对于给定页面的上一个常规页面。
Syntax
PAGE.PrevInSection
Returns
page.Page
Hugo 根据此排序层次结构对当前栏目的常规页面集合进行排序来确定 下一个 和 上一个 页面:
用于确定 下一个 和 上一个 页面的已排序页面集合独立于其他页面集合,这可能会导致意外行为。
例如,使用此内容结构:
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/section.html
{{ range .Pages.ByWeight }}
<h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
{{ end }}
layouts/page.html
{{ with .PrevInSection }}
<a href="{{ .RelPermalink }}">上一个</a>
{{ end }}
{{ with .NextInSection }}
<a href="{{ .RelPermalink }}">下一个</a>
{{ end }}当您访问 page-2 时:
PrevInSection方法指向 page-3NextInSection方法指向 page-1
要反转 下一个 和 上一个 的含义,您可以在 站点配置 中更改排序方向,或对 Pages 对象使用 Next 和 Prev 方法以获得更大的灵活性。
示例
通过检查页面存在性来进行防御性编码:
{{ with .PrevInSection }}
<a href="{{ .RelPermalink }}">上一个</a>
{{ end }}
{{ with .NextInSection }}
<a href="{{ .RelPermalink }}">下一个</a>
{{ end }}