Custom pagination is not working

I’m having some difficulty getting custom pagination working, and I’m not sure what I’m doing wrong. On my home/index page, I have a pagination of content, and up until now I’ve been using the default .Paginator. However, I want to use a custom one that filters out posts that have draft: true set in their parameters.

Currently, I’m doing $pag := .Paginate (where .Site.RegularPages "Params.hidden" "ne" "true" and then loop over all the pages range $pag.Pages. However, this includes a post with hidden: true in the front-matter. What am I doing wrong?

Thanks in advance!

It sounds like your front matter value is a boolean, but your where statement performs a string comparison.

https://gohugo.io/functions/where/#use-where-with-booleans

1 Like

Sorry for the late response, but I’ve been messing with this some more and can’t get it to work at all, I’m doing .Paginate (where .Site.RegularPages "Params.hidden" "ne" true). It just gives the same result as the default pagination.

I wanted to see if I could get any custom pagination working, so I tried {{ range (.Paginate .Site.Taxonomies.tags).Pages }}{{ .Title }}{{ end }} to see if I could get it to print out the names of some of my tags, but that did the same thing, just printing out the name of a few posts.

Any ideas what I’m doing wrong? I’m super confused. Thanks!

Try this

{{- $pag = .Paginate (where site.RegularPages "Params.hidden" "!=" true) }}
{{- range $pag.Pages }}
<!-- your code here -->
{{ end }}

Ensure the frontmatter parameter is hidden: true with no quotes around true.

Please post a link to the public repository for your project. Without seeing everything I would just be guessing.

Still no luck. The project repo is here; my theme is a submodule, source here. The pagination I’m having troubles with is here. Any help would be greatly appreciated! Thanks in advance. I probably missed out something small.

You are paginating twice:

The .Paginator is static and cannot change once created.

If you call .Paginator or .Paginate multiple times on the same page, you should ensure all the calls are identical. Once either .Paginator or .Paginate is called while generating a page, its result is cached, and any subsequent similar call will reuse the cached result. This means that any such calls which do not match the first one will not behave as written.

See:
https://gohugo.io/templates/pagination/#list-paginator-pages

1 Like

To get around the issue mentioned by @jmooring, replace your header.html with the code below

{{- $pagination := "" }}
{{- if .IsHome }}
{{ $pagination = .Paginate (where .Site.RegularPages "Params.hidden" "!=" true) }}
{{- end }}

{{ if and .IsHome (eq $pagination.PageNumber 1) }}
{{ .Site.Params.HomepageHeader | safeHTML }}
{{ else }}
<a href="/">&laquo; Home</a>
{{ end }}
1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.