Get the previous and next three posts to the current post based on weight (you can replace Weight
with Date
or any other parameter. Replace 3
with your number of choice and modify the code to your liking)
{{/* Get the current page, just to avoid context confusion later on. */}}
{{ $currentPage := . }}
{{/* Get all regular pages in the site. */}}
{{ $allRegularPages := where site.AllPages "Kind" "page" }}
{{/* Display the previous three pages by weight. */}}
{{/* There might be less than three, or zero, depending on weights. */}}
<h2>Previous three pages (by weight)</h2>
{{ range where $allRegularPages.ByWeight "Weight" "lt" $currentPage.Weight | last 3 }}
<h3><a href="{{ .RelPermalink }}">{{ .Title }}</a> (weight = {{ .Weight }})</h3>
{{ end }}
{{/* Display next three pages by weight. */}}
{{/* There might be less than three, or zero, depending on weights. */}}
<h2>Next three pages (by weight)</h2>
{{ range where $allRegularPages.ByWeight "Weight" "gt" $currentPage.Weight | first 3 }}
<h3><a href="{{ .RelPermalink }}">{{ .Title }}</a> (weight = {{ .Weight }})</h3>
{{ end }}
Credit: @jmooring