<p>Assuming that when you are on the first page, you don't show the last 3 entries of your structure and the single array you need is an array of entries, this should do it for you:</p>

<pre><code>{# Set how many entries to have around current entry #}
{% set surroundingEntries = 3 %}

{# Fetch prev 3 entries - reverse order because of limit() #}
{% set prevEntries = craft.entries.positionedBefore(entry).orderBy('lft desc').limit(surroundingEntries).all() %}

{# Fetch next 3 entries #}
{% set nextEntries = craft.entries.positionedAfter(entry).limit(surroundingEntries).all() %}

{# if there aren't enough nextEntries, add some #}
{% if nextEntries|length &lt; surroundingEntries %}
    {% set limit = surroundingEntries - nextEntries|length %}
    {% set extraEntries = craft.entries.section('sectionName').limit(limit).all() %}
    {% set nextEntries = nextEntries|merge(extraEntries) %}
{% endif %}

{# Put prevEntries back in order and merge nextEntries to it #}
{% set results = prevEntries | reverse | merge(nextEntries) %}
</code></pre>

<p>If you need an array of ids, replace <code>.all()</code> with <code>.ids()</code> and if I misunderstood you and  you need two separate arrays, simply don't merge them as <code>results</code> and use <code>nextEntries</code> / <code>prevEntries</code> but don't forget to <code>|reverse</code> your <code>prevEntries</code> to get them back in order :)</p>

CC BY-SA 4.0
2019-02-20T17:54:38.570Z
I couldn't get this to work for my specific example but is almost certainly how someone else might do it, so have marked it as the right answer :-)
Daniel
CC BY-SA 4.0
2019-02-20T18:08:46.913Z
@Daniel what's not right for your case?
Oli
CC BY-SA 4.0
2019-02-16T13:08:22.837Z
2019-02-16T14:45:08.140Z
2019-02-16T14:45:08.140Z
Oli
<p>For a given structure entry, I need to return the previous three, and the next three entries as one array. But if the given entry is the last in the structure, I need the "next three" to wrap around and select the first three entries, if that makes sense.</p>

<p>The given structure entry should not be included the results.</p>

<p>Does anyone know how I can achive this: all my efforts so far have failed or are far too complex.</p>

<craft3><structure>
Fetch the three entries before, and after, a given Structure entry, with wrapping
# Fetch the three entries before, and after, a given Structure entry, with wrapping

(Asked by: username_0 on 2019-02-15)

For a given structure entry, I need to return the previous three, and the next three entries as one array. But if the given entry is the last in the structure, I need the "next three" to wrap around and select the first three entries, if that makes sense.

The given structure entry should not be included the results.

Does anyone know how I can achive this: all my efforts so far have failed or are far too complex.

**Tags:** craft3, structure


---

## 1 answers

### Answer by username_1 on 2019-02-16. Score: 1 | Accepted answer

Assuming that when you are on the first page, you don't show the last 3 entries of your structure and the single array you need is an array of entries, this should do it for you:
{# Set how many entries to have around current entry #}
{% set surroundingEntries = 3 %}

{# Fetch prev 3 entries - reverse order because of limit() #}
{% set prevEntries = craft.entries.positionedBefore(entry).orderBy('lft desc').limit(surroundingEntries).all() %}

{# Fetch next 3 entries #}
{% set nextEntries = craft.entries.positionedAfter(entry).limit(surroundingEntries).all() %}

{# if there aren't enough nextEntries, add some #}
{% if nextEntries|length < surroundingEntries %}
    {% set limit = surroundingEntries - nextEntries|length %}
    {% set extraEntries = craft.entries.section('sectionName').limit(limit).all() %}
    {% set nextEntries = nextEntries|merge(extraEntries) %}
{% endif %}

{# Put prevEntries back in order and merge nextEntries to it #}
{% set results = prevEntries | reverse | merge(nextEntries) %}

If you need an array of ids, replace .all() with .ids() and if I misunderstood you and  you need two separate arrays, simply don't merge them as results and use nextEntries / prevEntries but don't forget to |reverse your prevEntries to get them back in order :)

> **Comments:**
> **username_0**: I couldn't get this to work for my specific example but is almost certainly how someone else might do it, so have marked it as the right answer :-)  
> **username_1**: @username_0 what's not right for your case?  

---

