Forum

Thread tagged as: Problem

Category pagination

I can't work out how to get pagination working with categories (in Runway, not sure about normal Perch)… The URL structure for a category is example.com/resources/category/category-name which is all working fine. The problem I'm having is when I try to paginate a list of resources in a particular category. I'd like it to look like this, for example: example.com/resources/cateogry/category-name/page/2

I've got the routing working, so when I manually write the URL in my browser it works. The routes I've set are:

  • resources/category/[slug:cat]
  • resources/category/[slug:cat]/page/[i:page]

Using `<perch:showall /> and going to the second page in the list of pages, I'm seeing

  • prev_url /resources/category/category-name/page/2
  • next_url /resources/category/category-name/page/2?page=3
  • prev_page_number 1
  • next_page_number 3

The prev_url and next_url values look wrong: shouldn't it be …/page/1 and …/page/3? (I'm on this page /resources/category/accessibility/page/2)

Anyway, I've got a bit of a work-around working, using conditionals and the individual bits that would make up the URL:

<a href="/resources/<perch:if exists="url_cat" />category/<perch:content id="url_cat" />/</perch:if>page/<perch:content id="next_page_number" encode="false" />" class="next">Next</a>

But this feels a bit unnecessary. Shouldn't this work with a rewrite attribute? Something like rewrite="/resources/{cat:category/%s/}{page:page/%s/}"?

I'm running into the same issues with Blog too, if that helps pinpoint what's going on. My routing there all works by hand – again, it's just prev_url and next_url that aren't doing what I expected in the template:

  • blog/year/[year:year]
  • blog/year/[year:year]/month/[i:month]
  • blog/category/[slug:cat]
  • blog/tag/[slug:tag]
  • blog/author/[slug:author]
  • blog/tag/[slug:tag]/page/[i:page]
  • blog/category/[slug:cat]/page/[i:page]
  • blog/year/[year:year]/page/[i:page]
  • blog/year/[year:year]/month/[i:month]/page/[i:page]

Thanks for taking a look and here are my diagnostics:

Perch Runway: 3.0.14, PHP: 7.1.8, MySQL: mysqlnd 5.0.12-dev - 20150407 - $Id: b396954eeb2d1d9ed7902b8bae237b287f21ad9e $, with PDO
Server OS: Darwin, cgi-fcgi
Installed apps: content (3.0.14), assets (3.0.14), categories (3.0.14), perch_blog (5.5.1), perch_forms (1.9)
App runtimes: <?php $apps_list = [ 'perch_blog', 'perch_forms', ];
PERCH_LOGINPATH: /cms
PERCH_PATH: /Users/martin/Sites/tempertemper.net/web/cms
PERCH_CORE: /Users/martin/Sites/tempertemper.net/web/cms/core
PERCH_RESFILEPATH: /Users/martin/Sites/tempertemper.net/web/cms/resources
Image manipulation: GD
PHP limits: Max upload 32M, Max POST 32M, Memory: 128M, Total max file upload: 32M
F1: 3b606135b33e6a102526838f4152a807
Resource folder writeable: Yes
HTTP_HOST: tempertemper.local
DOCUMENT_ROOT: /Users/martin/Sites/tempertemper.net/web
REQUEST_URI: /cms/core/settings/diagnostics/
SCRIPT_NAME: /cms/core/settings/diagnostics/index.php
Martin Underhill

Martin Underhill 5 points

  • 3 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

The prev_url and next_url values look wrong: shouldn't it be …/page/1 and …/page/3?

The pagination doesn't know what format you're using for your URLs. The router is passing in that page equals 3 or whatever, but it has no knowledge how to reconstitute that into the type of URL you want.

Anyway, I've got a bit of a work-around working, using conditionals and the individual bits that would make up the URL

That's not a workaround, that's just how you do it.

prev_url and next_url are a convenience that use a standard good practice URL format that will work without modification 9 time out of 10. If you want to go off-piste, that's absolutely fine and you have the tools to do that.

Ah, ok. Good to know the 'workaround' isn't a hack :D

I suppose using prev_url and next_url in my resources section is a bit off-piste. With the blog, though – isn't that the standard URL structure I'm using? I've just had a look at the Perch (as opposed to Runway) sites I look after with equivalent cat/tag/date grouping and they work ok with <perch:blog id="prev_url" encode="false" rewrite="/blog{cat:/category/%s}{tag:/tag/%s}{year:/year/%s}{page:/page/%s}"

The conditional thingy I'm using works fine with the blog, but I thought the rewrites would work too. Seems they're not in Runway but are in Perch. No big deal as I've got it working, but the docs suggest rewrite is an option: https://docs.grabaperch.com/templates/attributes/rewrite/ Is it something I'm doing wrong or is it something Runway isn't set up for?