1

Craft is no longer handling my pagination properly. This follows an update from somewhere around 3.4 to 3.7.X.

{% set entries = craft.entries.section(sectionName) 
    .orderBy('postDate DESC')
    .relatedTo(relatedElement)
    .with ( ['thumbnail'] )
    .limit(10)
    %}

sectionName and relatedElement are set earlier in the template. I'm getting correct URLs, like:

  • /blog/category/rens-nrens/
  • /blog/category/rens-nrens/p2 and the route I have set up is working, but Craft keeps showing me the first page of results.

{% paginate entries as pageInfo, pageEntries %}

...

{% if pageInfo.totalPages > 1 %}
<div class="pagination">
More:
<a href="{{ pageInfo.firstUrl }}" title="First page of news">First</a>
{% for page, url in pageInfo.getPrevUrls(3) %}
  <a href="{{ url }}">{{ page }}</a>
{% endfor %}

<span class="active">{{ pageInfo.currentPage }}</span>

{% for page, url in pageInfo.getNextUrls(3) %}
  <a href="{{ url }}">{{ page }}</a>
{% endfor %}
<a href="{{ pageInfo.lastUrl }}" title="Last page of news">Last</a>
</div>  
{% endif %} 

...

{% set entries = craft.entries.section(sectionName) 
.orderBy('postDate DESC')
.relatedTo(relatedElement)
.with ( ['thumbnail'] )
.limit(10)
%}

{% for item in entries %}
various other bits...
  <p>{{ item.teaser }}</p>

{% endfor %}

{% paginate entries as pageInfo, pageEntries %}

5
  • Can you show the code you use to output the entries? Do you use the {% paginate entries as pageInfo, pageEntries %} tag? Jun 9, 2022 at 7:21
  • @MaxStrebel Yes. I just updated my post with the code.
    – 4midori
    Jun 9, 2022 at 19:52
  • Thanks for updating. But where do you actually output the posts? I'm missing a {% for entry in pageEntries %} ... {% endfor %}which actually outputs the entries. what you show is just the pagination, right? On another note: did you try to explicitly set pageTrigger in the general config settings? craftcms.com/docs/3.x/config/config-settings.html#pagetrigger Jun 9, 2022 at 20:39
  • @MaxStrebel I updated the code further, above. I did not set pageTrigger, because I am letting Craft apply its defaults.
    – 4midori
    Jun 10, 2022 at 17:29
  • Hey 4midori, thanks for the additional code. I answered with a solution hopefully. On your last comment: while it is good to trust on defaults, you might just want to check once if setting the (pageTrigger) parameter explicitly maybe fixes it. In this case thats not the problem, just a thought. Jun 12, 2022 at 7:05

1 Answer 1

2

According to your code, I think the issue is that you loop through entries instead of pageEntries.

<!-- Just set this query once. Rest is done by the paginate tag -->
{% set entries = craft.entries.section(sectionName) 
    .orderBy('postDate DESC')
    .relatedTo(relatedElement)
    .with ( ['thumbnail'] )
    .limit(10)
%}

<!-- here you set the paginate query. Use pageInfo for infos of the current page, the pageEntries gives you an object that contains the entries of the current page. So instead of "for entry in entries" you use "for entry in pageEntries" -->
{% paginate entries as pageInfo, pageEntries %}

<!-- Your pagination as it is, looks fine -->
{% if pageInfo.totalPages > 1 %}
  <div class="pagination">
    ...
  </div>  
{% endif %}

<!-- here you should loop through the entries you set in the paginate tag instead of a second query. Use the pageEntries (from {% paginate %}) object instead of entries, to show the entries for this page. -->
{% for item in pageEntries %}
  <p>{{ item.teaser }}</p>
{% endfor %}
1
  • That was it! Thanks so much.
    – 4midori
    Jun 13, 2022 at 19:03

Your Answer

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

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