Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

I have a structure like below and need previous / next links on an entry such that they go back and forth between E, F, G and H. They shouldn't step into D or I.

I've been trying this, but I can't get it not to step into D or I.

{%  set prev = entry.getPrev({section: 'mySection'}) %}

    {% if not prev %}
        {% set prev = craft.entries.section('mySection').last() %}
    {% endif %}

    {{ prev.title }}

    {% set next = entry.getNext({section: 'mySection'}) %}

    {% if not next %}
        {{ craft.entries.section('mySection').order('lft desc').first().title }}
    {% endif %}

    {{ next.title }}
  • A
    • B
    • C
    • D
      • E
      • F
      • G
      • H
  • I
    • J
    • K
    • L
      • M
      • N
      • O
      • P
share|improve this question
add comment

1 Answer 1

up vote 5 down vote accepted

Craft provides many helpful methods to traverse structure sections and your use case is no exception.

Instead of using .getPrev() or .getNext(), try .getPrevSibling() or .getNextSibling() (or their aliases .prevSibling and .nextSibling.)

You could create prev/next links with something like this:

{% set prev = entry.prevSibling %}
{% if prev %}
    <a href="{{ prev.url }}">{{ prev.title }}</a>
{% endif %}

{% set next = entry.nextSibling %}
{% if next %}
    <a href="{{ next.url }}">{{ next.title }}</a>
{% endif %}
share|improve this answer
    
Thanks for your help Stuart. Much appreciated! –  user1070143 Jun 20 at 9:50
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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