I’m implementing series taxonomy into my blog, and I want to generate a Previous and Next in series buttons. I’ve gotten close, however I can’t seem to get the if logic to work properly due to my limited knowledge of Hugo.
With the below code I have been able to generate the links, but they are always the first and last post in the series due to the ordering by date and the first 1 limitation. Problem is without those, it will generate a next link for every post that follows (and same with previous).
single.html
{{ $currentNode := . }}
<p class="text-center">Read more posts in {{ .Params.series }}</p>
{{ range first 1 (where .Site.Pages.ByDate "Params.series" .Params.series) }}
{{ if ne $currentNode .}}
{{ if gt $currentNode.Date.Unix .Date.Unix}}
<a class="previous-post" href="{{ .Permalink }}">Previous Post</a>
{{ end }}
{{ end }}
{{ end }}
{{ range first 1 (where .Site.Pages.ByDate.Reverse "Params.series" .Params.series) }}
{{ if ne $currentNode .}}
{{ if lt $currentNode.Date.Unix .Date.Unix}}
<a class="next-post" href="{{ .Permalink }}">Next Post</a>
{{ end }}
{{ end }}
{{ end }}
Actually, I’m pretty sure that the where isn’t working, because I’m seeing links appear on posts that don’t have a series configured. So I don’t think I’m very close.
Basically I want to recreate the templateVars .NextInSection and .PrevInSection, but by taxonomy “series” and the term that matches the current page’s series value.
Because Section is defined as the top level under content, I can’t easily use .PrevInSection, .NextInSection because I want it to live under blog still. I almost need a .NextInSlug, or .NextOfSameType.
I’m interested in a solution to this. I’ve only been playing with Hugo for a couple months, @doctorallen, but my current workaround for this was to just add a series_no: to the front matter, but I’m not sure that a) this fits your use case or b) is practically if you have an exceptionally large number of entries in a particular series.
I’m currently using the following in single.html to generate the Previous & Next links in a series of posts: {{ if .NextInSection }} <a href="{{.NextInSection.Permalink}}">Previous Post</a> {{ end }} {{ if .PrevInSection }} <a href="{{.PrevInSection.Permalink}}">Next Post</a> {{ end }}
@alexandros
This doesn’t work as it displays a link to the next post under the section, which in the documentation is defined as the top level folder directly under content:
And I only want the links to show up on ness and stuff pages, and they only have links to posts under “happy” but that is considered the “slug” and not a Section. So basically you need a .NextInSubSection, or .NextInSlug variable.
Sorry for digging so deep in history, but I solved that problem.
I used that to generate blog series links on my blog: www.itchyfeet.pl (blog is in polish)
My piece of code is following:
{{ range where .Site.Pages.ByDate "Params.series" .Params.series }}
{{ if gt $.Date.Unix .Date.Unix }}
{{ $.Scratch.Set "previous" .Permalink }}
{{ end }}
{{ end }}
{{ range where .Site.Pages.ByDate.Reverse "Params.series" .Params.series }}
{{ if lt $.Date.Unix .Date.Unix }}
{{ $.Scratch.Set "next" .Permalink }}
{{ end }}
{{ end }}
<ul class="actions pagination">
<li><a href="{{ if $.Scratch.Get "previous" }}{{ $.Scratch.Get "previous" }} {{ end }}" class="{{ if not ($.Scratch.Get "previous") }}disabled {{ end }}button big previous">Next</a></li>
<li><a href="{{ if $.Scratch.Get "next" }}{{ $.Scratch.Get "next" }} {{ end }}" class="{{ if not ($.Scratch.Get "next") }}disabled {{ end }}button big next">Previous</a></li>
</ul>