Prevent Prev Next To Another Section Post

Dear All

I have “blog” section and “quote” section like this →

/content/
    |__/blog/
        |__/_index.md
        |__/my-first-blog-post.md
        |__/my-second-blog-post.md
    |__/quote/
        |__/_index.md
        |__/my-first-quote-post.md
        |__/my-second-quote-post.md
    |__/_index.md

Everytime I visit “My Second Blog Post” and click Next, it continue to “My First Quote Post”. How to prevent that?
So when I click Next on the last post of current section, it just stop there without continue to another section post?

And this is pagination code on my layouts/_default/single.html →

{{ if eq .Section "blog" }}
<div class="pagination">
<a class="prev" href="{{ if .PrevPage }}{{ .PrevPage.Permalink }}{{ else }}#{{ end }}">« Prev</a>
<a class="next" href="{{ if .NextPage }}{{ .NextPage.Permalink }}{{ else }}#{{ end }}">Next »</a>
</div>
{{ end }}

Regards

Try this

        {{- if in .Section "blog" -}}
        {{- $p := where site.RegularPages.ByDate.Reverse "Section" "blog" -}}
        <div class="pagination">
          {{ with $p.Prev . }}
        <a class="prev" href="{{ .Permalink }}">« Prev</a>
        {{ end }}
        {{ with $p.Next . }}
        <a class="next" href="{{ .Permalink }}">Next »</a>
        {{ end }}
        {{ end }}
        </div>

If you need to show the title, replace prev and next with {{ .Title }}.

I tend to declare my prev and next and then test against them in cases similar to these which eliminates needing to specify which section you’d be in.

{{$next := .Next }}
{{$prev := .Prev }}
{{if $prev}}
  {{if ( eq $prev.Section .Section)}}
    <a class="prev" href="{{ $prev.Permalink }}">« Prev</a>
  {{end}}
{{end}}
{{if $next}}
  {{if (eq $next.Section .Section)}}
    <a class="next" href="{{$next.Permalink }}">Next »</a>
  {{end}}
{{end}}

Your code throws an error

 execute of template failed: template: : executing "main" at <$prev.Section>: nil pointer evaluating page.Page.Section

fixed. Alternatively I probably could have used this if I wanted to be more concise.


{{ $next := .Next  | default . }}
{{ $prev := .Prev  | default . }}
{{ if and ( eq $prev.Section .Section) (ne $prev .) }}
  <a class="prev" href="{{ $prev.Permalink }}">« Prev</a>
{{ end }}
{{ if and (eq $next.Section .Section) (ne $next . ) }}
   <a class="next" href="{{$next.Permalink }}">Next »</a>
{{ end }}

1 Like

It works. But one of the button disappear.
I need both button still there and prevent them continue to another section.

I modified to this code but one of the button still disappear.

{{ $prev := .Prev  | default . }}
{{ $next := .Next  | default . }}
<div class="pagination">
{{ if and (eq $prev.Section .Section) (ne $prev .) }}
<a class="prev" href="{{ if $prev }}{{ $prev.Permalink }}{{ else }}#{{ end }}">« Prev</a>
{{ end }}
{{ if and (eq $next.Section .Section) (ne $next .) }}
<a class="next" href="{{ if $next }}{{ $next.Permalink }}{{ else }}#{{ end }}">Next »</a>
{{ end }}
</div>

If that is the case you should wrap the Href value with the if statements like so.

{{ $next := .Next  | default . }}
{{ $prev := .Prev  | default . }}
<a class="prev" href="{{ if and (eq $prev.Section .Section) (ne $prev .) }}{{ $prev.Permalink }}{{ end }}">« Prev</a>
<a class="next" href="{{ if and (eq $next.Section .Section) (ne $next .) }}{{$next.Permalink }}{{ end }}">Next »</a>
1 Like

Works perfectly. Thanks.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.