Looping through pages on same level


I am trying to loop through all the pages on the same level as the current page the user is on. Grav docs say I can use ‘@self.siblings’ to achieve this, so I have written the following:

{% for page in page.self.siblings %}
      <a href="{{ page.url }}">
         {{ page.menu }}
{% endfor %}

However, when I load the page, no items come up in the navigation bar. Anyone know how I can fix this? Thank you!

I use this:

{% for page in page.children %}

        {% if page.visible %}
            <li><a href="{{ page.url }}">{{ page.menu }}</a></li>
        {% endif %}
    {% endfor %}


Thanks for the response! However, this only shows the children of the current page, not the pages on the same level.

Should be just page.siblings not page.self.siblings … try that…

page.siblings is not working either. I have also tried pages.siblings :frowning:

you are using same variable page for item and array, try this:

{% for p in page.siblings %}
        {% if p.visible %}
            <li><a href="{{ p.url }}">{{ p.menu }}</a></li>
        {% endif %}
    {% endfor %}

Thanks for the reply, but still no items are showing up in the navbar :\

try enabling debug and add a {{ dump(page.siblings) }} you should see the pages array in debug bar. dont forget to clear cache

The console says “null” when I do a {{ dump(page.siblings) }}.

ugg… Page.siblings() is not a valid function. it’s actually a method on the Collection object. Bit hackish but you can do:

{{ dump(page.parent.children.remove(page.path)) }}

I tried {{ dump(page.parent.children.remove(page.path)) }}, but it didn’t give me a siblings array. How can I get page.siblings to work, or loop through all the pages on one level?

Ok I’ll just have to break down and actually try this. :slight_smile:

Works for me:

Oh I got it!! Turns out I had a typo (facepalm). Thank you so much for your time!

I hate those!!