Is it possible to use Pagination with the Related function?

I’ve been looking around for examples on how to use pagination with the related function, but had no luck so I’ve been trying to figure it out for the past few hours.

Here’s what I’ve got so far. The following code gives all the related content. No issues.

{{$related := (site.RegularPages).Related .}}
{{with $related}}
  {{range .}}
   {{.Title}}
  {{end}}
{{end}}

But when I try to add .Paginate to the code as in the docs, that’s where I run into issues.

{{ $paginator := .Paginate ((site.RegularPages).Related .) }}
{{ template "_internal/pagination.html" . }}
{{ range ($paginator.Pages)}}
   {{ .Title }}
{{ end }}

This also does not work:

{{ range (.Paginate (site.RegularPages)).Related .}}

For reference, I’m not using pagination anywhere else.

In config.toml, I have paginate = 5

I believe the issue has to do with the dot and context? Not sure.

From Pagination | Hugo

Hugo supports pagination for your homepage, section pages, and taxonomies.


Is it possible to use Pagination with the Related function?

NopeYep. :slight_smile:

Yes you can, but it’s restricted to the list type of pages. But I guess there are two variants here: 1) To Paginate the related pages or 2) Find related pages for the paginated pages. Both should be possible but I guess you want 2).

This should work:

{{ $pag := .Paginate site.RegularPages }}
{{ $pages := $pag.Pages }}
{{ $related := $pages.Related }}

Or variants of the above.

2 Likes

Thanks bep. That was helpful.

I’m actually after the first version. The following sort of works:

{{$related := .Paginate ((where site.RegularPages "Section" .Section).Related .)}}
{{range $related.Pages}}
{{.Title}}
{{end}}

What I mean by sort of, is that it works fine with 940 posts, but if I add more to the content folder, I get the following error message:

<.Paginate>: error calling Paginate: invoked multiple times with different arguments

It’s hard for me to judge exactly what’s going on in your case, but there can only be one paginator for a given list page (Hugo is static), so I added that error – because it’s a common mistake to try to paginate multiple page sets.

What Hugo version are you running?

For sure. I understand.

I’ve been troubleshooting for a bit now. I have over 2000 posts, and it seems the error has nothing to do with the quantity of posts or pagination being called multiple times.

The error was happening because for some reason, the paginator did not like the keywords in two of my posts. In one post I had, keywords = ["a", "b"], and in the other keywords = ["c", "d"]. Nothing wrong with this front matter that I can tell – I’m using these exact keywords in other posts – but that’s why the error was occurring. If I removed “b” and “d” from keywords in those two posts, no error and pagination worked as expected.

Here’s the kicker, upgrading from v0.53 to the latest version fixed the error! So perhaps a weird glitch in the previous version?

Thanks again for helping out.

1 Like

OK, now I remember. I fixed an issue with sorting or related pages. I added the page name as a last sort argument to get stable sorting for related pages with same date + keywords. This explains the behaviour you see.

1 Like