{% paginate %} tag takes current subpapage number from url address. Can this behaviour be bypassed and page number be passed directly into {% paginate %} tag?
- 1,286
- 7
- 18
2 Answers
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,380
- 9
- 25
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,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
pageInfovariable: https://docs.craftcms.com/v3/dev/tags/paginate.html#the-pageinfo-variable) – August Miller Nov 19 '18 at 21:15pageInfovariables would be wrong, for example url of next subpage. – Piotr Pogorzelski Nov 21 '18 at 17:49