While I know the back button does this perfectly well, I was wondering if there was a good way to "return to" a paginated page using a previous request or something? Wanting to do this server-side rather than relying on JS. Thanks.

  • Can you elaborate a bit? E.g. are you linking from a paginated entry listing to an entry's actual URL? – Brandon Kelly Jun 30 '14 at 20:28
  • 2
    Precisely. A blog list view that is paginated. Say you're on pg 5 and choose an entry I want to go back to pg 5 with a text link on entry view versus just the blog landing page. Wasn't sure if it was in the request or session arrays or not. If not, I will make a small plugin. – Ryan Shrum Jun 30 '14 at 20:47

The easiest way would be to check craft.request.urlReferrer and see if they just came from the entry index page. If so, provide a link to send them back.

{% set referrer = craft.request.urlReferrer %}
{% set indexBaseUrl = url('articles') %}

{% if referrer and referrer|slice(0, indexBaseUrl|length) == indexBaseUrl %}
    <a href="{{ referrer }}">Go back</a>
{% endif %}

Another, more explicit way to do it would be to actually pass the current page in the query string of the entry URLs, from the entry index page.

{% for entry in entries %}
    {% set url = url(entry.uri, { page: craft.request.pageNum }) %}
    <a href="{{ url }}">{{ entry.title }}</a>
{% endfor %}

Then you could check for that “page” query string param in the single entry template:

{% set indexPage = craft.request.getQuery('page') %}
{% if indexPage %}
    {% set indexUrl = url('articles/' ~ craft.config.pageTrigger ~ indexPage) %}
    <a href="{{ indexUrl }}">Go back</a>
{% endif %}
| improve this answer | |

This is not something built into Craft, but you could accomplish this with a plugin. You could have it run during your plugin's init function. On each page load, it would take the current page and load it into a currentUrl session variable. Then when you went to another page, the previous page url would move from currentUrl to a prevUrl session variable, and the current one would go into currentUrl.

You would write a template variable to check and output them in your templates. You could possibly work with Craft's session flashes, but you would have to remember to tell it not to delete them when you grabbed them.

| improve this answer | |

I'd guess if this existed it would be available through craft.session. Unfortunately, I don't see any reference to a variable for the previous page's URL in the documentation for craft.session:


| improve this answer | |

Was looking for an answer and came up with an noob alternative.

On the paginated page (simplified):

<a href="{{entry.url}}/p{{pageInfo.currentPage}}">{{entry.title}}></a>

This will parse the page segment as the last segment of the entry. I first tried to use getSegment, but page numbers are seen as pagination url's and therefore ignored as segments. So, on the entry page if it's a paginated section for instance:

<a href="/{{entry.section.handle}}/p{{craft.request.getPageNum()}}">back</a>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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