3

{% paginate %} tag takes current subpapage number from url address. Can this behaviour be bypassed and page number be passed directly into {% paginate %} tag?

2 Answers 2

2

If you're looking to display a set of Elements that simulate a paginated query, you might actually be looking for the offset Query parameter.

Here's an example of fetching a “page” of results based on a number of entriesPerPage and the number of pagesToSkip:

{% set customPageOfEntries = craft.entries()
    .offset(entriesPerPage * pagesToSkip)
    .limit(entriesPerPage)
    .all() %}

Of course, this doesn't include the ability to see how many pages of results you have, or to generate links to other pages of results.

3
  • Thank you for reply. Unfortunetly I cant use this method - i still have to use actual paginate tag, because I need "pageInfo" to create pagination links.
    – piotrpog
    Nov 18, 2018 at 19:34
  • I think I might need some more context—you can still paginate the normal set of results, separately, and just run this query off to the side (and use information in your pageInfo variable: docs.craftcms.com/v3/dev/tags/…) Nov 19, 2018 at 21:15
  • I guess i could do that, but some of pageInfo variables would be wrong, for example url of next subpage.
    – piotrpog
    Nov 21, 2018 at 17:49
2

Recently i needed such thing again and this time i foud out how to do this:

{% set query = craft.entries %}

{% set paginator = create('craft\\db\\Paginator', [query, {
    pageSize: 3,
    currentPage: craft.app.request.pageNum,
}]) %}

{% set pageInfo = create('craft\\web\\twig\\variables\\Paginate').create(paginator) %}

{% set pageResults = paginator.getPageResults() %}

To manually set result page, you need to pass some number to currentPage instead of default value taken from URL. Note that initial query cannot have limit param, this needs to be passed as pageSize param to paginator class.

1
  • OMG that is so incredibly useful - I am building a Nuxt/Craft app and needed some api calls to paginate. It works perfectly. You are a champion! This should be marked as the answer. Mar 10 at 15:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.