<p>First – you're passing a string value (<code>'posts'</code>) to the <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> methods, which will produce unreliable results.</p>

<p>The <code>getNext()</code> and <code>getPrev()</code> methods actually expect either an <a href="https://craftcms.com/docs/templating/elementcriteriamodel" rel="nofollow noreferrer">ElementCriteriaModel</a> instance, or an object representing an ElementCriteriaModel's attributes (i.e. a query like <code>{ section: 'posts' }</code> – not a string, number or any other, non-iterable value.</p>

<p>I'm guessing <code>'posts'</code> is a section handle, but Craft won't know that and it will just ignore the string and <em>return any element matching the current element's type</em> - so if you call <code>entry.getNext('posts')</code>, you're probably going to get an entry back, but it won't reliably be an entry from the <code>'posts'</code> section; it could be an entry from <em>any</em> section in your system.</p>

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

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

<p>If this code is in an entry template, you could even do something like this:</p>

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

<p>An additional tidbit: The only instance where it's ok to call <code>getNext()</code> and <code>getPrev()</code> without passing an ECM (or pass an invalid value, like a string), is whenever your <code>entry</code> variable is already part of an element result set – for instance, whenever you're looping on the result of a <code>craft.entries</code> query:</p>

<pre><code>{% for entry in craft.entries.section('posts') %}
    ...
    {% set next = entry.getNext() %}
    {% if next %}
        &lt;a href="{{ next.url }}"&gt;{{ next.title }}&lt;/a&gt;
    {% endif %}
{% endfor %}
</code></pre>

<p>As for the error message you're seeing:</p>

<p>The <code>getNext()</code> and <code>getPrev()</code> methods should always be wrapped in a conditional, because <em>they won't realibly, always return an element</em> (i.e., there might not <em>be</em> a "next" or "previous" entry in your current context):</p>

<pre><code>{% set prev = entry.getNext(criteria) %}
{% set next = entry.getPrev(criteria) %}

{% if prev %}
    &lt;a href="{{ prev.url }}"&gt;{{ prev.title }}&lt;/a&gt;
{% endif %}

{% if next %}
    &lt;a href="{{ next.url }}"&gt;{{ next.title }}&lt;/a&gt;
{% endif %}
</code></pre>

<p>The error message you're getting, is simply stating that there is no <code>url</code> attribute for one of your <code>next</code> or <code>prev</code> variables – meaning that those variables don't actually refer to valid elements, because the <code>getNext()</code> and/or <code>getPrev()</code> methods returned a NULL value. Simply wrap your code in conditionals, and the error will go away.</p>

<p>If you want to make sure that you're always displaying both a "next" and a "previous" link, you can make the links "loop" by having them point to the <em>first</em> and <em>last</em> entries respectively, whenever there isn't a next or previous entry in the set:</p>

<pre><code>{% set criteria = craft.entries.section('posts') %}
...
{% set next = entry.getNext(criteria) ?? criteria.first() %}
{% set prev = entry.getPrev(criteria) ?? criteria.last() %} 
...
{% if next %}
    &lt;a href="{{ next.url )}"&gt;{{ next.title }}&lt;/a&gt;
{% endif %}
</code></pre>

CC BY-SA 3.0
2016-12-09T00:25:47.587Z
2016-12-09T00:43:14.853Z
2016-12-09T00:43:14.853Z
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> 
This is the standard craft reference what I use all the time. If you use this it will work!</p>

CC BY-SA 3.0
2016-12-08T22:25:43.670Z
This is for a single entry, not a page of entries.
Hellyar
CC BY-SA 3.0
2016-12-08T22:38:15.773Z
And it also did not work
Hellyar
CC BY-SA 3.0
2016-12-08T22:08:07.933Z
2016-12-08T22:08:07.933Z
Lennard
<p>I have setup chevrons to links to the previous and next posts.  It seems to work for all but a handful of posts.  Those posts generate the following template error:</p>

<blockquote>
  <p>Impossible to access an attribute ("url") on a null variable</p>
</blockquote>

<pre><code>        &lt;p class="post-meta"&gt;
            {% set prev = entry.getPrev( 'posts' ) %}
            {% set next = entry.getNext( 'posts' ) %}

        &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>The specific line the template error references is</p>

<p><code>&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;</code></p>

<p>There are previous entries to access.  </p>

<p>Thanks</p>

<templating><error-message>
Why do some entries generate a template error on get next or previous post when others do not?
# Why do some entries generate a template error on get next or previous post when others do not?

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

I have setup chevrons to links to the previous and next posts.  It seems to work for all but a handful of posts.  Those posts generate the following template error:

Impossible to access an attribute ("url") on a null variable

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

        
        
        
        {{ entry.postDate | date('F d, Y') }}  
        
        
    

The specific line the template error references is

There are previous entries to access.  
Thanks

**Tags:** templating, error-message


---

## 2 answers

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

First – you're passing a string value ('posts') to the getNext() and getPrev() methods, which will produce unreliable results.
The getNext() and getPrev() methods actually expect either an ElementCriteriaModel instance, or an object representing an ElementCriteriaModel's attributes (i.e. a query like { section: 'posts' } – not a string, number or any other, non-iterable value.
I'm guessing 'posts' is a section handle, but Craft won't know that and it will just ignore the string and return any element matching the current element's type - so if you call entry.getNext('posts'), you're probably going to get an entry back, but it won't reliably be an entry from the 'posts' section; it could be an entry from any section in your system.
So here's how your code probably should look:
{% set criteria = { section: 'posts' } %}
{% set prev = entry.getPrev(criteria) %}
{% set next = entry.getNext(criteria) %}
...

If this code is in an entry template, you could even do something like this:
{% set criteria = { section: entry.section } %}
{% set prev = entry.getPrev(criteria) %}
...

An additional tidbit: The only instance where it's ok to call getNext() and getPrev() without passing an ECM (or pass an invalid value, like a string), is whenever your entry variable is already part of an element result set – for instance, whenever you're looping on the result of a craft.entries query:
{% for entry in craft.entries.section('posts') %}
    ...
    {% set next = entry.getNext() %}
    {% if next %}
        {{ next.title }}
    {% endif %}
{% endfor %}

As for the error message you're seeing:
The getNext() and getPrev() methods should always be wrapped in a conditional, because they won't realibly, always return an element (i.e., there might not be a "next" or "previous" entry in your current context):
{% set prev = entry.getNext(criteria) %}
{% set next = entry.getPrev(criteria) %}

{% if prev %}
    {{ prev.title }}
{% endif %}

{% if next %}
    {{ next.title }}
{% endif %}

The error message you're getting, is simply stating that there is no url attribute for one of your next or prev variables – meaning that those variables don't actually refer to valid elements, because the getNext() and/or getPrev() methods returned a NULL value. Simply wrap your code in conditionals, and the error will go away.
If you want to make sure that you're always displaying both a "next" and a "previous" link, you can make the links "loop" by having them point to the first and last entries respectively, whenever there isn't a next or previous entry in the set:
{% set criteria = craft.entries.section('posts') %}
...
{% set next = entry.getNext(criteria) ?? criteria.first() %}
{% set prev = entry.getPrev(criteria) ?? criteria.last() %} 
...
{% if next %}
    {{ next.title }}
{% endif %}


---

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

Take a look at this: <https://craftcms.com/docs/templating/paginate>
This is the standard craft reference what I use all the time. If you use this it will work!

> **Comments:**
> **username_0**: This is for a single entry, not a page of entries.  
> **username_0**: And it also did not work  

---

