I want to walk a user through a series of steps 1-4, which are children of a section page for hierarchical navigation purposes. However, I am starting the instructions on the section-1 page. The .Prev page of step-2 is step-3, and I would like the .Prev page of section-1 to be step-2. Is there a way I can override this, perhaps by manually setting next/prev in the front matter?
You can simply specify the weight for pages, see Next | Hugo for details.
Or sorting current section’s pages in some way, such as custom parameter other than weight. I’m using this approach for my special use case (docs’s sidebar navigation) to get rid of messing up the weight.
You can sort any pages collection you want, and find the current page position, then determind next and prev.
Pseudocode:
{{/* Change the page collection below to meet your case. */}}
{{ $pages := .Section.Pages }}
{{ $pages = $pages | append (where site.Pages "Section" "blahblahblah") }}
{{/* Sorting collection in some way, just for an example. */}}
{{ $pages = sort $pages "some-param" "asc" }}
{{ $pos := -2 }} // current page position on collection
{{ range $i, $page := $pages }}
{{ if eq $page.RelPermalink $.RelPermalink }}
{{ $pos = $i }}
{{ break }}
{{ end }}
{{ end }}
{{ with index $pages (sub $pos 1) }}
Prev
{{ end }}
{{ with index $pages (add $pos 1) }}
Next
{{ end }}
As @razon mentioned, instead of the Prev and Next methods on a Page object, use the Prev and Next methods on a Pages object (they are very different things):
In your example, the navigation would look something like this:
{{ with append .CurrentSection.Pages (slice .CurrentSection) }}
{{ with .Next $ }}
<a href="{{ .RelPermalink }}">Previous</a>
{{ end }}
{{ with .Prev $ }}
<a href="{{ .RelPermalink }}">Next</a>
{{ end }}
{{ end }}
Yes, I know that the labels in the above are reversed. This is covered in the documentation, and stems from a difference of opinion regarding what “next” and “previous” mean. In my view this falls into the “regrettable historic error” category.
Here’s a working example:
git clone --single-branch -b hugo-forum-topic-50076 https://github.com/jmooring/hugo-testing hugo-forum-topic-50076
cd hugo-forum-topic-50076
hugo server
For anyone who finds this link later: it turns out the Hextra theme does exactly what I wanted, in its pager component. It overrides the pager to check if prev or next are set in the page params, and uses them (instead of .Prev / .Next) if so.