Getting different number of items per page

My template uses two calls to “range”, one in the index and another for the list at /posts/

Index.html:

{{ $loop := (.Paginate (where (where .Data.Pages "Type" "!=" "page") ".Params.unlisted" "!=" true ) 1).Pages }}
{{ range $loop }}
	{{ .Render "li" }}
{{ end }}

list.html

{{range (.Paginate (where (where .Site.Pages.ByDate.Reverse "Type" "!=" "page") ".Params.unlisted" "!=" true )  9).Pages }}
	{{ .Render "summary"}}
{{ end }}

The index shows one post, like expected.

/post/page/1/ lists one post.
/post/page/2/ and 3 list six posts.
/post/page/4/ lists eight posts.

From then onward it feels even more random. With pages showing 9 posts, and some showing 3 or something like that. The full source code is on github https://github.com/brunoamaral/future-imperfect-DI

Any ideas on why this happens?

If you remove the filtering with where in layouts/post/list.html, does the issue still happen?

So for instance testing:

{{range (.Paginate (where .Site.Pages.ByDate.Reverse "Type" "!=" "page") 9).Pages }}
	{{ .Render "summary"}}
{{ end }}

And

{{range (.Paginate .Site.Pages.ByDate.Reverse 9).Pages }}
	{{ .Render "summary"}}
{{ end }}

Perhaps (with the emphasis on perhaps) goes something wrong with the filtering on .Params.unlisted. Say paginate start with 9 posts for the list page, but 8 of them have that front matter variable set, will you then get a paginate page with just one post?
(Just thinking aloud here, I don’t know how Hugo processes multiple where functions on a paginate page behind the scenes).

Maybe… I don’t know much about how the where function works either. I’ll run the test you suggest and report back as soon as I can :slight_smile:

Alright, I was testing it and came up with a solution that i’m not sure why it works.

I’m using the same variable to store the posts, like so:

 {{ $loop := (.Paginate (where (where .Data.Pages "Type" "!=" "page") ".Params.unlisted" "!=" true ) 1).Pages }}

I repeat that line on the post/list.html file, setting the limit of posts to 9.

Still, would like to know the reasoning behind this. :man_shrugging:

I’m glad you found a workaround! I’m also not sure why this works, but I’m glad it does because I was out of ideas for something that would work. :slight_smile:

Happy new year!

1 Like