Forum

Thread tagged as: Problem, Runway, Blog

Blog pagination URLs

Blog post pagination isn't working for anything other than the main listing. I've set up all the routes in the archive.php page's page options: thoughts/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], etc.

/blog/year/2014?page=2 and blog/year/2014/page/2 (etc.) work ok, but the pagination isn't hooking up to them.

I've tried using the rewrite attribute but it's not working. Here's my template:

<perch:if exists="paging">
  <div class="paging">
    <p class="page">Page <perch:blog id="current_page" /> of <perch:blog id="number_of_pages" /></p>
    <perch:if exists="not_first_page">
      <a href="<perch:blog id="prev_url" encode="false" rewrite="/blog{cat:/category/%s}{tag:/tag/%s}{year:/year/%s}{page:/page/%s}" />" class="previous">Previous</a>
    </perch:if>
    <perch:if exists="not_last_page">
      <a href="<perch:blog id="next_url" encode="false" rewrite="/blog{cat:/category/%s}{tag:/tag/%s}{year:/year/%s}{page:/page/%s}" />" class="next">Next</a>
    </perch:if>
  </div>
</perch:if>

Am I doing something wrong or is this a known bug?

Martin Underhill

Martin Underhill 5 points

  • 5 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Is it the case that the links are wrong, or that the route isn't matching?

I think it's the route that isn't matching. {cat:/category/%s}{tag:/tag/%s}{year:/year/%s} isn't returning anything at all, but {page:/page/%s} is, so I guess it's because the routing isn't finding the cat, tag and year (and I guess month if I was using that as a filter) in the query string.

The links seem ok: if I manually write out the URL it finds the page. e.g. example.dev/blog/archive?year=2014&page=2

Here are my routes for the archive page, in case that's helpful:

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]

Weirdly, if I put in the url manually like /blog/archive?year=2014&page=2 the 'next' button in the pagination picks up the query string parameters and the link it gives me is /blog/year/2014/page/3 and page 3 returns the right posts. But the 'next' button on page 3 just takes me back to /blog/page/4 again.

So it seems like the pagination doesn't have access to the routing url's query parameters…

Drew McLellan

Drew McLellan 2638 points
Perch Support

I'm still unclear as to whether it's routing or the link output.

If you view source on the HTML, is the value of the href attribute in the format you want?

The next button has a value of href="/blog/page/2" when I'm on the https://localhost:8080/blog/year/2014 page. I'd've expected https://localhost:8080/blog/year/2014/page/2. Is that what you mean?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok, great. If you remove the rewrite attribute completely (to establish what we're working with) what does it output for the href ?

Going from https://localhost:8080/blog/year/2014, the href is returning href="/blog/year/2014?page=2" when the rewrite attribute is removed.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Ah, ok, interesting. So the year parameter is already part of the URL, so won't be matched by the rewrite attribute.

What does this give you?

rewrite="{page:page/%s/}"

That outputs href="page/2/". Weirdly this points to https://localhost:8080/blog/year/page/2/, so it's not keeping the 2014 from the url. (Still on this page: https://localhost:8080/blog/year/2014)

Drew McLellan

Drew McLellan 2638 points
Perch Support

I'm going to have to delve deep on this one. I can see the issue, but I'm not immediately sure of the solution.

Cool. If you need me to do any testing or anything like that just shout. I'm @tempertemper on the public Slack team :)