Approaches for next/previous by series or author


#1

I’m wondering what the recommended practice is for using series to navigate between posts. I started using a “series” value in the front matter because I wanted to organize some posts and use tags and categories for other purposes. The solution was something a front matter with:

+++
date = "2014-12-08T12:51:43-06:00"
title = "Lorem Ipsum"
series = "Foo Bar"
+++

And a template that has logic to decide what the next/previous posts are for the series:

{{ if .Params.series }}
	{{ $.Scratch.Set "series_links"     0 }}
	{{ $.Scratch.Set "series_previous" "" }}
	{{ $.Scratch.Set "series_next"     "" }}
	{{ range where .Site.Pages.ByDate "Params.series" .Params.series }}
		{{ if gt $.Date.Unix .Date.Unix }}
			{{ $.Scratch.Add "series_links" 1 }}
			{{ $.Scratch.Set "series_previous" .Permalink }}
		{{ end }}
	{{ end }}
	{{ range where .Site.Pages.ByDate.Reverse "Params.series" .Params.series }}
		{{ if lt $.Date.Unix .Date.Unix }}
			{{ $.Scratch.Add "series_links" 1 }}
			{{ $.Scratch.Set "series_next" .Permalink }}
		{{ end }}
	{{ end }}

	{{ if gt ($.Scratch.Get "series_links") 0 }}
		<ul>
			{{ with $.Scratch.Get "series_previous" }}<li><a href="{{ . }}">Previous in Series</a></li>{{ end }}
			{{ with $.Scratch.Get "series_next"     }}<li><a href="{{ . }}">Next in Series</a></li>{{ end }}
		</ul>
	{{ end }}
{{ end }}

At the time I started doing this, tags and categories didn’t seem to be the right fit. And it made it kind of easy to glop this onto a “next/previous post by this author” too, since that was just a quick change from Params.series to Params.author.

Is there a better way (or different way) to do this with Hugo now?