Can I set next and prev in front matter?

Say I have a set of pages

├── section-1/
│   ├──
│   ├── step-2/
│       ├──
│   ├── step-3/
│       ├──
│   ├── step-4/
│       ├──

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.

I think they are not in the same section however, as one has a different parent to the rest?

You can sort any pages collection you want, and find the current page position, then determind next and prev.


{{/* 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) }}
{{ end }}
{{ with index $pages (add $pos 1) }}
{{ 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 hugo-forum-topic-50076
cd hugo-forum-topic-50076
hugo server
1 Like

Would this be a massive breaking change if it was addressed?

Yes, it would.