Just wanted to give a nice Twig example on how to solve a problem I often ran into:
Pagination of a Looper Provider
TLDR; use
{{ (query.current_page-1)*9 }}
in the Offset field of the Looper Provider.
Explanation
Of course, you can create a “simple” Archive Layout without a Looper Provider for a Custom Post Type. In fact Themeco’s docs and videos recommend to not use a Looper Provider in an Archive Layout.
Let’s assume we have built a Custom Post Type with ACF for Events. The Events do have a start date and an end date.
Correct me if I am wrong with the following statement:
If you want to use Meta Values i.e. to not show Events that lies in the past, or order the Events by a Meta Value like a custom ACF date field then you need a Looper Provider.
The current behavior with pagination and a Looper Provider is:
No matter what pagination page you are on, you always get the same first 9 items of the query (in my example 9, cause that is my current setting in WordPress > Reading).
Now one way you can achieve pagination with Twig is seen in the following screenshot:
Here I use the Meta Values field to show only Events whose start date lies in the future and not in the past. And I also use the Meta Value to sort the Events by their start date in the Order By field.
So the idea of this approach is to re-set the Offset value in the Looper Provider on every pagination page. If I’m on pagination page 1 set the offset to 0, if I’m on page 2 set the offset to 9, on page 3 set it to 18 etc.
Luckily, the {{dc:query_current_page}}
does work correct and returns the current paginated page you are on. So if you take this value, subtract one and multiply it by 9 you have the offset value you can use for pagination.
So with {{ ({dc:query:current_page}-1)*9 }}
in the Offset field of the Looper Provider pagination is working.
Already love the Twig feature, great job! Making calculations in dynamic data alone is a really cool feature.
Edit: as @charlie mentioned in the post below it is better to use Twig syntax only like this:
{{ (query.current_page-1)*9 }}
This is possible since every dynamic content can also be accessed with Twig syntax. Just remove the dc:
and exchange the colons :
with dots .
So dc:query:current_page
becomes query.current_page
.