Return Previous/Next pages based on a parameter

Hugo .18

Consider a section of content called “authors” where each author’s page has a link to the previous and next author.

The docs give me:

.PrevInSection Pointer to the previous content within the same section (based on pub date).

Noting that it’s “based on pub date,” but I’d like to create those pointers based on a front matter value (specifically, in this case, lastname).

Doesn’t seem like from the docs what I’d like to do is available out-of-the-box, but is there a workaround anyone knows about, or if I made this content section a taxonomy, are there super powers that allow this, that I’ve missed?

And, if appropriate, I can submit a feature request on Github. I don’t know golang, but would be happy to at least start a pr.

1 Like

Not sure how documented it is, we disscussed this as being a little bit confusing years ago (mainly because it goes round and round and is a little hard to use …), but Pages (the page collection in Hugo) has two methods named Prev and Next.

There are many built-in page collections in Hugo (.Site.Pages, the .Data.Pages) etc., but then there are the page collections you get from the where func. etc., which I assume you can use to use to filter by author or whatever.

See hugolib package - github.com/spf13/hugo/hugolib - Go Packages

Cool, thank you. I was a bit hesitant to post about something I hadn’t tried coding, but this is a good starting place, so I’ll see what I can come up with.

Sure, but note that you can do all of this in the templates, no need for any Go coding.

1 Like

I believe you can also use weight for Prev and Next or PrevInSection and NextInSection right, @bep? It’s hard to write code samples from an iPhone, but @budparr I think you could use {{range where “Section” “Authors”}} and then sort by param. When I get to a laptop, I’ll try some templating as well since I think I know what you’re going for…

1 Like

Also look at the built-in pagination system. If you set the paginate size = 1 and order by author, you will get a pretty good solution.

1 Like

Hi budparr,

I am trying to solve the same challenge you’ve described here.
Unfortunately, I have had no luck fashioning templates with the desired output using the Hugo core template functions.

Were you successful? If so, would you mind sharing your approach here?
Thank you!

Hey, @emiller - I don’t think I ever did solve it the way I wanted to, but I’ve also not been working on that project lately and intend to again soon. I’ll try to remember to post something here if I do.

Working gist: https://gist.github.com/rordi/eab8606b377638eeaa984367bde0a2d0

1 Like