3

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

Piotr Pogorzelski
  • 1,286
  • 7
  • 18

2 Answers2

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.

August Miller
  • 3,380
  • 9
  • 25
  • 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. – Piotr Pogorzelski Nov 18 '18 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: https://docs.craftcms.com/v3/dev/tags/paginate.html#the-pageinfo-variable) – August Miller Nov 19 '18 at 21:15
  • I guess i could do that, but some of pageInfo variables would be wrong, for example url of next subpage. – Piotr Pogorzelski Nov 21 '18 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.

Piotr Pogorzelski
  • 1,286
  • 7
  • 18
  • 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. – bhu Boue vidya Mar 10 '22 at 15:00