3

I have an index page that filters entries based on url query strings, eg: site.com/inductees?county=123.

I put together a very simple pagination setup with next/prev links. However, when I click "next" I am brought to site.com/inductees/p2 and I lose my filtered set of entries from the ?county=123 bit.

How can I get pagination working and keep the query string in my url?

Thanks!

2
  • 2
    Wouldn't it be an option to add the additional parameters to the url <a href="{{ pageInfo.prevUrl }}?country=123">Previous Page</a> Commented Dec 12, 2017 at 21:10
  • That was excitingly simple. I was thinking of that before I asked this question but assumed it wouldn't work for some reason. Thanks! Make it an answer if you'd like a vote. Commented Dec 12, 2017 at 21:18

2 Answers 2

3

The huge advantage of craft over other CMS is the high flexibility and the ability to customize many things. Thus Craft won't provide non changeable <a> tags but links you can change like you want.

{% set country = craft.request.getParam('country') %}
<a href="{{ pageInfo.prevUrl }}?country={{country}}">Previous Page</a>
9

In most Craft projects I do I'll create a pagination include file so anything I need that requires pagination, I can just include that and I get the same pagination pattern anywhere within my site when I need it.

While you do have to set the country attribute with each link, sometimes it might get a little tedious to do this manually or you may expand your attributes to include other options, like ?country=123&distance=50.

This is where Craft's craft.request.getQueryStringWithoutPath() comes into play. In Craft 3 this has been changed to craft.app.request.queryStringWithoutPath. But you could do something like this:

{% if craft.request.getQueryStringWithoutPath() %}
  {% set queryString = "?" ~ craft.request.getQueryStringWithoutPath() %}
{% endif %}

<a href="{{ paginate.prevUrl }}{% if queryString is defined %}{{ queryString }}{% endif %}">Previous</a>

Then no matter what URL attributes you have, they'll always be included within your pagination links.

1
  • Thanks for the tip! I did use a pagination include file on another, more straight-forward project. This project is an older one, but lots of areas need streamlining... this will help! Commented Dec 13, 2017 at 15:39

Your Answer

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

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