我需要完成布局,并带有指向下一个和上一个帖子的链接,以及一个标记实际帖子的计数器,前面的表示不好:
<previousPost 10/100 nextPost>
除了提到当前的帖子编号之外,我已将所有工作都发挥了作用
{{ $posts := (where .Site.RegularPages "Section" "==" "news") }}
{{ $postCount := len $posts }}
{{ $postCount }}
{{ if .PrevInSection }}
<a href="{{.PrevInSection.Permalink}}">Prev Post</a>
{{ end }}
{{ I have no idea }}/{{ $postCount }}
{{ if .NextInSection }}
<a href="{{.NextInSection.Permalink}}">Next Post</a>
{{ end }}
但是我不知道如何在netxInSection
上下文中找到页面的值。我正在考虑将代码更改为range
并使用index
标记当前页面,但是我认为这应该是一种更聪明的方法。
谢谢!
答案 0 :(得分:0)
一些头撞在墙上后,我找到了一种方法...
{{ range $index, $element := (where .Site.RegularPages "Type" "news" ).Reverse }}
{{ if eq . $ }}
{{- $.Scratch.Set "currentItem" (add $index 1) }}
{{ end }}
{{end}}
{{ $posts := (where .Site.RegularPages "Section" "==" "news") }}
{{ $postCount := len $posts }}
{{ if .PrevInSection }}
<a href="{{.PrevInSection.Permalink}}" class="page-navigation__prev">prev</a>
{{ else }}
<a href="" disabled class="page-navigation__prev">prev</a>
{{ end }}
<div class="page-navigation__counter">
{{ $.Scratch.Get "currentItem" }}/{{ $postCount }}
</div>
{{ if .NextInSection }}
<a href="{{.NextInSection.Permalink}}" class="page-navigation__next">next</a>
{{ else }}
<a href="" disabled class="page-navigation__next">next</a>
{{ end }}