Number of posts om infinite scroll

Hi,

I have implementes the infinite scroll hooks for a news page on a client website.

https://arcticbioscience.vercel.app/news/

The problem is that I want it to get 9 posts per page, not 10.

I have set the number of posts to 9 here, but I still fetches 10. How do I solve this?

Hi @kasper,

That setting in the Admin Reading Settings only determines the number of posts shown using a traditional PHP-based theme. It doesn’t affect what is returned in the WP REST API.

To control how many posts are displayed on each page I think you need to add the per_page parameter to the request. See here for more info.

I think to be able to use the Infinite Scroll hooks here, the solution goes through the creation of a handler that performs a custom request by using the low-level method libraries.source.api.get() which allows to specify params such as per_page fro the request

@cristianbote Any idea how to combine a custom handler that return a custom number of elements per archive page with an Infinite Scroll hook such as useArchiveInfiniteScroll ?

Hey @juanma, was looking into it but fell short of a code solution. Gonna ask @David for his opinion on this one.

So, @David, what are our thoughts? Can you combine the useArchiveInfiniteScroll with a custom handler to have custom pagination?

It should be possible, because useArchiveInfiniteScroll works with links and uses the next property of archive data objects to get the next link, whatever the number of posts of each archive page.

Hey @juanma, another option would be specifying per_page for all the REST API requests using the state.source.param property, though that would change the number of posts for all archives.

1 Like