<p>I believe I already answered this in the OP's <a href="https://craftcms.stackexchange.com/questions/18074/why-do-some-entries-generate-a-template-error-on-get-next-or-previous-post-when/18079#18079">other, similar question</a>, but:</p>

<p>You shouldn't pass a string value (i.e. <code>'posts'</code>) to <a href="https://craftcms.com/docs/templating/entrymodel#getNext" rel="nofollow noreferrer"><code>getNext()</code></a> and <a href="https://craftcms.com/docs/templating/entrymodel#getPrev" rel="nofollow noreferrer"><code>getPrev()</code></a> – those methods expect either an <a href="https://craftcms.com/docs/templating/elementcriteriamodel" rel="nofollow noreferrer">ElementCriteriaModel</a> instance, or an object with attributes representing a query (i.e. something like <code>{ section: 'posts' }</code>.</p>

<p>What actually happens when you call <code>entry.getPrev('posts')</code>, is that Craft will just <em>ignore</em> the string value (I'm guessing <code>'posts'</code> 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 <code>entry</code> – it can be an entry from <em>any</em> section in your system, which is probably where your problem is.</p>

<p>So here's how your code should probably look:</p>

<pre><code>{% set criteria = { section: 'posts' } %}
{% set prev = entry.getPrev(criteria) %}
{% set next = entry.getNext(criteria) %}
</code></pre>

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

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

<p>...and of course, you can make the criteria dynamic:</p>

<pre><code>{% set criteria = { section: entry.section } %}
{% set prev = entry.getPrev(criteria) %}
...
</code></pre>

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

<pre><code>{% set prev = entry.getPrev(criteria) %}
...
{% if prev %}
    &lt;a href="{{ prev.url }}"&gt;{{ prev.title }}&lt;/a&gt;
{% endif %}
</code></pre>

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

<pre><code>{% set next = entry.getNext(criteria) ?? craft.entries(criteria).first() %}
</code></pre>

CC BY-SA 3.0
2016-12-09T00:33:36.197Z
2016-12-09T00:39:09.330Z
2017-04-13T12:51:00.253Z
Mats Mikkel Rummelhoff
<p>Take a look at this: <a href="https://craftcms.com/docs/templating/paginate" rel="nofollow noreferrer">https://craftcms.com/docs/templating/paginate</a> 
I use this all the time and it will work then. </p>

CC BY-SA 3.0
2016-12-08T22:25:13.887Z
This is for a singe entry. Not pagination.
Hellyar
CC BY-SA 3.0
2016-12-08T22:38:00.177Z
And it also did not work
Hellyar
CC BY-SA 3.0
2016-12-08T22:15:35.763Z
2016-12-08T22:15:35.763Z
Lennard
<p>I have a related question regarding previous and next throwing a template error for some entries.  This is similar, but different.</p>

<p>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.  </p>

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

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

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

<p>Thanks</p>

<templating><error-templates>
Wrong urls printed for small subset using getNext and getPrev
# Wrong urls printed for small subset using getNext and getPrev

(Asked by: username_0 on 2016-12-08)

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' ) %}
        
        
        
        {{ entry.postDate | date('F d, Y') }}  
        
        
    `

Thanks

**Tags:** templating, error-templates

---

## 2 answers

### Answer by username_1 on 2016-12-09. Score: 2 | Accepted answer

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 %}
    {{ prev.title }}
{% 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() %}


---

### Answer by username_2 on 2016-12-08. Score: 1

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

> **Comments:**
> **username_0**: This is for a singe entry. Not pagination.  
> **username_0**: And it also did not work  

---

