Can't get page.earlier/later to work

Hi all,

I would like to create a “Next” and “Previous” link for the pages in a section, but I can’t figure out how to make it work. Right now I have this:
<p><a href="{{ page.earlier | safe }}">Next</a></p>
in my page template, but this only gives me an [object], not a path to the earlier page. I have set sort_by to "date". I thought I might be able to do page.earlier.permalink but this throws an error:
Reason: Variable `page.earlier.permalink` not found in context while rendering

Grateful for any help!


I think you need to wrap it with an if. Otherwise you run into a problem on the earliest page.

{% if page.earlier %}
    <a href="{{ page.earlier.permalink | safe }}">Next</a>
{% endif %}
1 Like

Thank you very much! Makes perfect sense…