Skip to content
● Meet Grav Guard: keep every Grav site you run secure and up to date, all from one place. Free for up to 3 sites. Read more →

Menu with highlight current page and parent title

Started by Muut Archive 11 years ago · 2 replies · 783 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 Muut Archive, 10 years ago
1 674 1 week ago
Archive · by Deleted User, 9 years ago
0 3043 9 years ago
Archive · by Muut Archive, 9 years ago
2 1983 9 years ago
Archive · by Muut Archive, 9 years ago
2 5337 9 years ago
Archive · by Muut Archive, 9 years ago
1 4134 9 years ago