Skip to content
Grav 2.0 is officially stable. Read the announcement →
Archive

Looping through pages on same level

Started by Muut Archive 10 years ago · 14 replies · 911 views
10 years ago

Hello,

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:

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

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

10 years ago

I use this:

TWIG
{% for page in page.children %}

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

HTH

10 years ago

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

10 years ago

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

10 years ago

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

10 years ago

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

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

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

10 years ago

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

10 years ago

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

10 years ago

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

TWIG
{{ dump(page.parent.children.remove(page.path)) }}
10 years ago

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?

10 years ago

Ok I'll just have to break down and actually try this. :)

10 years ago

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

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1359 9 years ago
Archive · by Muut Archive, 9 years ago
2 936 9 years ago
Archive · by Muut Archive, 9 years ago
2 4066 9 years ago
Archive · by Muut Archive, 9 years ago
1 2957 9 years ago
Archive · by Muut Archive, 9 years ago
3 1121 9 years ago