Skip to content
● Grav 2.2 is out: 2x faster cold starts and 96% less memory on big sites. Read the announcement →

Menu with highlight current page and parent title

Started by Muut Archive 11 years ago · 2 replies · 745 views
11 years ago

Its working :-)

TWIG
<h3>{{ page.parent.title }}</h3>

<ul>

    {% for p in page.parent.children %}

        {% if p == page %}
            <li class="active"><a href="{{p.url}}">{{ p.title }}</a></li>
        {% else %}
            <li><a href="{{p.url}}">{{ p.title }}</a></li>
        {% endif %}

    {% endfor %}

</ul>
11 years ago

Oeps... its is also showing invisible and disabled pages.
How can I solve that issue?

11 years ago

Hi @ocean12, you can add some checks to your loop. The page class has many methods to fine-grain your result. To only show visible and maybe published pages change your for loop to

---twig
{% for p in page.parent.children if p.routable and p.visible %}
...
{% endfor %}

TXT


Here `routable` accounts that the page must have a route and `visible` that it must appear in the menu. You can drop the latter, if you don't need it.

Sommerregen

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 2607 9 years ago
Archive · by Muut Archive, 9 years ago
2 1712 9 years ago
Archive · by Muut Archive, 9 years ago
2 5008 9 years ago
Archive · by Muut Archive, 9 years ago
1 3838 9 years ago
Archive · by Muut Archive, 9 years ago
3 1819 9 years ago