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

Menu with highlight current page and parent title

Started by Muut Archive 10 years ago · 2 replies · 614 views
10 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>
10 years ago

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

10 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 1360 9 years ago
Archive · by Muut Archive, 9 years ago
2 939 9 years ago
Archive · by Muut Archive, 9 years ago
2 4069 9 years ago
Archive · by Muut Archive, 9 years ago
1 2958 9 years ago
Archive · by Muut Archive, 9 years ago
3 1122 9 years ago