<p>After trying a few more things, I realised I needed the <code>descendantOf</code> parameter to get this working.</p>

<p>This is what my code looks like:</p>

<pre><code>{% set prevEntry = craft.entries.descendantOf(entry.parent).positionedBefore(entry).order('lft desc').first() %}
{% if prevEntry is null %}
  {% set prevEntry = entry.siblings.last() %}
{% endif %}

{% set nextEntry = craft.entries.descendantOf(entry.parent).positionedAfter(entry).first() %}
{% if nextEntry is null %}
  {% set nextEntry = entry.siblings.first() %}
{% endif %}
</code></pre>

<p>This restricts the <code>positionedBefore</code> and <code>positionedAfter</code> parameters to only work with entries that have the same parent as the current entry.</p>

<p>If there is no previous or next entry, I link to the first or last sibling entry instead, creating a loop.</p>

CC BY-SA 3.0
2016-05-05T15:44:29.740Z
2016-05-05T15:44:29.740Z
Katrin
<p>I want to link to enabled sibling entries within a structure and am struggling to get it working.</p>

<p>This is what my structure looks like:</p>

<p><img src="https://i.sstatic.net/uEnG3.png" alt=""></p>

<p>Because I only want to fetch entries that are enabled, I followed the docs instructions and fetch previous entries like this:</p>

<pre><code>{% set prevEntry = craft.entries.positionedBefore(entry).order('lft desc').first() %}
</code></pre>

<p>With this, when on the <code>Prince Phillip</code> entry, I thought that would return <code>null</code> for <code>prevEntry</code>, but it is returning the <code>Altynia Asylmoratova</code> entry.</p>

<p>How can I fetch previous and next entries that are <strong>direct siblings and enabled</strong>?</p>

<previous-next>
Return previous or next enabled sibling of same parent
# Return previous or next enabled sibling of same parent

(Asked by: username_0 on 2016-05-05)

I want to link to enabled sibling entries within a structure and am struggling to get it working.
This is what my structure looks like:

Because I only want to fetch entries that are enabled, I followed the docs instructions and fetch previous entries like this:
{% set prevEntry = craft.entries.positionedBefore(entry).order('lft desc').first() %}

With this, when on the Prince Phillip entry, I thought that would return null for prevEntry, but it is returning the Altynia Asylmoratova entry.
How can I fetch previous and next entries that are direct siblings and enabled?

**Tags:** previous-next


---

## 1 answers

### Answer by username_0 on 2016-05-05. Score: 7 | Accepted answer

After trying a few more things, I realised I needed the descendantOf parameter to get this working.
This is what my code looks like:
{% set prevEntry = craft.entries.descendantOf(entry.parent).positionedBefore(entry).order('lft desc').first() %}
{% if prevEntry is null %}
  {% set prevEntry = entry.siblings.last() %}
{% endif %}

{% set nextEntry = craft.entries.descendantOf(entry.parent).positionedAfter(entry).first() %}
{% if nextEntry is null %}
  {% set nextEntry = entry.siblings.first() %}
{% endif %}

This restricts the positionedBefore and positionedAfter parameters to only work with entries that have the same parent as the current entry.
If there is no previous or next entry, I link to the first or last sibling entry instead, creating a loop.


---

