2

TL;DR: Looking for something like paginate_base parameter in EE, but for Craft.

My site's homepage has content in tabs for different sections (news, products, etc.) and in each tab I would like to have pagination links that would go to 2nd, 3rd, etc page in each section.

So in News tab I display news entries and would like to link to /news/p2, /news/p3 etc, on Products tab I would have links to /products/p2, /products/p3 etc. All that on same page, with URL different from /news or /products

Is there any way to achieve that in Craft?

2 Answers 2

3

Not tested, but I would assume that you could simply get the total entries, divide by the pagination limit and then generate your dynamic nav using a range loop.

{% set totalEntries = craft.entries.section('news').limit(null).total() %}
{% set pageLimit = 10 %}
{% set totalPages = (totalEntries / pageLimit)|round(0, 'ceil') %}

<ul>
    {% for i in range(1, totalPages) %}
        <li><a href="/news/p{{ i }}"></a></li>
    {% endfor %}
</ul>
4
  • Good idea and might work in some cases (having replaced find() with total()) - but does not really work for me as the increment in for` is adding 1, while it needs to add pageLimit - and if I add pageLimit as 3rd parameter for range I get range(): step exceeds the specified range error as initially number of entries is less then number of item per page. I need to get this done on Craft level, not on Twig level. Jan 27, 2016 at 8:07
  • Just to be clear pageLimit is the limit that you have defined for your elementCriteriaModel used by the pagination tag on the target page (i.e. on the news page). So by definition totalEntries/pageLimit rounded up should represent the total number of pages. The only other task is to create the same number of links as pages — thus the range loop. What is not working? Jan 28, 2016 at 9:17
  • You're right, I just did not apply parameter to total() that I have in my entries loop, and so was getting unexpectable results. Your answer works, I only changed it to range(2, totalPages) (as I don't need first page) and added {% if totalEntries > pageLimit %} because otherwise range is counting in reverse direction. Thank you very much! Jan 28, 2016 at 11:21
  • Great! I'm glad it worked. fyi... i updated answer with your .total() suggestion — much cleaner and probably saves some memory. Cheers. Jan 28, 2016 at 18:22
3

Since version 3.1.28, you can set base URL of pagination links, like this:

{% paginate query as pageInfo, pageEntries %}
{% do pageInfo.setBasePath('new_url') %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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