4

I got an entry template from a structured section.
With prev / next nav links at entry bottom.
But the next sibling is deactivated.
So the page return is 404 not found !

{% set prev = entry.prevSibling %}  
{% set next = entry.nextSibling %}

{% if prev %}
    <a class="nav-prev nav-left" href="{{ prev.url }}"><&nbsp;{{ prev.title }}</a>
{% endif %}
{% if next %}
    <a class="nav-next nav-right" href="{{ next.url }}">{{ next.title }}&nbsp;></a>
{% endif %}

Should it be status=live by default no?

2 Answers 2

7

The getNextSibling documentation says:

getNextSibling() will return the next sibling whether or not it’s enabled. If you want to get the closest enabled sibling, you can do this instead:

{% set next = craft.entries.positionedAfter(entry).order('lft asc').first() %}

Similarly for getPrevSibling to get the previous enabled sibling you can do:

{% set prev = craft.entries.positionedBefore(entry).order('lft desc').first() %}
2
  • This 'getNextSibling' thing is something I've stumbled over a couple time. Thanks for the post Marion. Commented Dec 8, 2015 at 22:09
  • I'd suggest adding ".siblingOf(entry)" into those suggestions so you only get siblings. Otherwise this answer gives you the next entry regardless of hierarchy :)
    – JamesNZ
    Commented Mar 7, 2017 at 20:58
5

Just an updated version of Marion's answer for Craft CMS 3 since order and first are deprecated.

{% set prev = craft.entries.positionedBefore(entry).orderBy('lft desc').one() %}

{% set next = craft.entries.positionedAfter(entry).orderBy('lft asc').one() %}

Your Answer

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

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