1

I have a related question regarding previous and next throwing a template error for some entries. This is similar, but different.

A few entries in the same section depict the wrong previous and next ent. Instead of linking to the previous or next entry they link to the About or 404 page. It only impacts 2 or 3 out of 100 entries - but always the same 2-3 entries.

There are no templates errors - just the wrong urls being printed for a small subset of the entries. Here is the code...

` {% set prev = entry.getPrev( 'posts' ) %} {% set next = entry.getNext( 'posts' ) %}

        <!-- previous post -->
        <span><a href="{{ prev.url }}"><i class="fa fa-chevron-left" aria-hidden="true"></i></a></span>
        <!--date of post -->
        <span>{{ entry.postDate | date('F d, Y') }} </span> 
        <!-- next post -->
        <span><a href="{{ next.url }}"><i class="fa fa-chevron-right" aria-hidden="true"></i></a></span>
    </p>`

Thanks

2 Answers 2

2

I believe I already answered this in the OP's other, similar question, but:

You shouldn't pass a string value (i.e. 'posts') to getNext() and getPrev() – those methods expect either an ElementCriteriaModel instance, or an object with attributes representing a query (i.e. something like { section: 'posts' }.

What actually happens when you call entry.getPrev('posts'), is that Craft will just ignore the string value (I'm guessing 'posts' is a section handle, but there's no way for Craft to know that), and you're probably going to get an EntryModel back, but it won't reliably be from the same section as your current entry – it can be an entry from any section in your system, which is probably where your problem is.

So here's how your code should probably look:

{% set criteria = { section: 'posts' } %}
{% set prev = entry.getPrev(criteria) %}
{% set next = entry.getNext(criteria) %}

You can also include other attributes, like for example order (otherwise, default attributes will apply, such as postDate desc for a Channel section, or structure for a Structure):

{% set criteria = { section: 'posts', order: 'title asc' } %}
{% set prev = entry.getPrev(criteria) %}
...

...and of course, you can make the criteria dynamic:

{% set criteria = { section: entry.section } %}
{% set prev = entry.getPrev(criteria) %}
...

Finally, make sure to always wrap the returned values from getNext() and getPrev() in a conditional – you can't always be sure that there'll be a valid "next" or "previous" element for any given entry, so this avoids template errors:

{% set prev = entry.getPrev(criteria) %}
...
{% if prev %}
    <a href="{{ prev.url }}">{{ prev.title }}</a>
{% endif %}

Of course, it's possible to declare a fallback value whenever there isn't a next or previous element:

{% set next = entry.getNext(criteria) ?? craft.entries(criteria).first() %}
1

Take a look at this: https://craftcms.com/docs/templating/paginate I use this all the time and it will work then.

2
  • This is for a singe entry. Not pagination.
    – Hellyar
    Dec 8, 2016 at 22:25
  • And it also did not work
    – Hellyar
    Dec 8, 2016 at 22:38

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.