雨果NextInSection与计数器

时间:2019-02-27 17:57:36

标签: hugo

我需要完成布局,并带有指向下一个和上一个帖子的链接,以及一个标记实际帖子的计数器,前面的表示不好:

<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标记当前页面,但是我认为这应该是一种更聪明的方法。

谢谢!

1 个答案:

答案 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 }}