3

I have managed to sort most Craft CMS questions out by reading the docs and feedback on StackExchange but am having issues with this one.

I have a page with a list of entries down the right side and the main entry content to the left.

The list is paginated. When I go to say page 2 of the pagination and click on an entry, the page reloads with the page 1 pagination list.

How can I get the page to remember that the entry belongs to page 2?

{% paginate craft.entries.section('orchard').limit('5').id('not ' ~ entry.id)  as entriesOnPage %}

 {% for entry in entriesOnPage %}
   {% for asset in entry.mediaThumb %}
     <div class="shuntalittle">
      <h6><a href="{{ entry.url }}">{{ entry.title }}</a></h6>
      <a href="{{ entry.url }}">
        <img src="{{ asset.getUrl('thumbTransform') }}" alt="{{ asset.title }}">
      </a>
     </div>
   {% endfor %}    
 {% endfor %}

  <div class="pagination-centered shuntalittle">
    <ul class="pagination">
      {% if paginate.prevUrl %}
        <li><a href="{{ paginate.prevUrl }}" class="arrow">&laquo;</a></li>
      {% endif %}
      {% for page, url in paginate.getPrevUrls(5) %}
        <li><a href="{{ url }}">{{ page }}</a></li>
      {% endfor %}
      <li class="current"><a href="">{{ paginate.currentPage }}</a></li>
      {% for page, url in paginate.getNextUrls(5) %}
        <li><a href="{{ url }}">{{ page }}</a></li>
      {% endfor %}
      {% if paginate.nextUrl %}
        <li><a href="{{ paginate.nextUrl }}" class="arrow">&raquo;</a></li>
      {% endif %}
    </ul>
  </div>
{% endpaginate %}

Many thanks, Alex

1

You would need to pass the current pagination page number with your entry links and than make Craft respond to URLs in a format like this: example.com/my-section/p2/my-entry-slug.

I guess this is more complicated than you'd think, as this is probably not possible to do with the default paginate tag and its preset routes. And I also don't know of a way to combine the paginate tag's page token with custom routes.

So I believe there's no other choice left than replacing paginate with a custom solution replicating its functionality.

This is possible with dynamic routes and some logic in your templates. So you'd first have to set up a route with a [page] and a [slug] token, which you can then access in your template.

my-section/p[page]/[slug] and
my-section/p[page]

I'll give some example code (untested and incomplete), to help you get started with the template.

All the pagination links build upon the route's page variable used in craft.entries's offset and limit parameters. With custom routes you also have to query for your entry entry model manually, I set it up to get the page's first entry if there's no slug variable set from your route.

{# Set params #}
{% set params = {section: 'orchard'} %}
{% set entriesPerPage = 5 %}

{% if page is defined %}
    {% set offsetParam = (page - 1) * entriesPerPage %}
    {% set pageSegment = '/p' ~ page %}
{% else %}
    {% set offsetParam = 0 %}
    {% set pageSegment = '' %}
{% endif %}

{# Get the entry model #}
{% if slug is defined %}
    {% set entry = craft.entries.slug(slug).first() %}
{% else %}
    {% set entry = craft.entries(params).offset(offsetParam).first() %}
{% endif %}

{# Get pagination params #}
{% if page is defined %}
    {% set entriesOnNextPage = craft.entries(params).offset(offsetParam + 1) %}

    {% if entriesOnNextPage|length %}
        {% set nextPageSegment = '/p' ~ page + 1 %}
    {% endif %}

    {% if page == 1 %}
        {% set prevPageSegment = '' %}
    {% else %}
        {% set prevPageSegment = '/p' ~ page - 1 %}
    {% endif %}
{% else %}
    {% set entriesOnNextPage = craft.entries(params).offset(entriesPerPage) %}

    {% if entriesOnNextPage|length %}
        {% set nextPageSegment = '/p2' %}
    {% endif %}
{% endif %}

<article>
    <h2>{{ entry.title }}</h2>
</article>

<nav>
    {% set entriesOnPage = craft.entries(params).offset(offsetParam).limit(entriesPerPage) %}

    {% for entry in entriesOnPage %}
        {% set image = entry.mediaThumb.first() %}

        <a href="/my-section{{ pageSegment }}/{{ entry.slug }}">
            <img src="{{ image.url }}">
        </a>
    {% endfor %}

    {% if prevPageSegment is defined %}
        <a href="/my-section{{ prevPageSegment }}">Previous Page</a>
    {% endif %}

    {% if nextPageSegment is defined %}
        <a href="/my-section{{ nextPageSegment }}">Next Page</a>
    {% endif %}
</nav>
1

Thank you so much for your detailed response Carl. As I was working to implement this solution I decided to go back and take a look at url segments (I am used to ExpressionEngine). I found that craft.request has a whole series of properties including getPageNum() which gets the pagination page number from the url.

I changed the urls link in my pagination list to:

<a href="{{ entry.url }}/p{{ craft.request.getPageNum() }}">

and that maintained the pagination position in the url which basically does what I need.

Cheers,

Alex

  • That's great you found this simple solution. So, Craft is able to match the route example.com/my-section/my-entry/p2 to an entry with the slug my-entry and also gets the pagination page from the URL. Too bad you can't configure it to have the URLs more hierarchically correct as in example.com/my-section/p2/my-entry. – carlcs May 19 '15 at 7:32

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.