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 →

Pages in multiple categories -> active menu item

Solved by johannes t View solution

Started by johannes t 9 years ago · 2 replies · 1371 views
9 years ago

Hi,

My menu looks like this: home, projects, blog, about ...

if i create a new page under projects it should also appear in blog.
This already works using collections in my blog

YAML
    content:
    items:
        - s[email protected]
        - taxonomy@:
                category: project

i use Route Overrides - Route Aliases
so e.g. in "/projects/example_project/item.md" i have "blog/example_project" as Route Alias,
so both urls

/projects/example_project

and

/blog/example_project

work.

now finally to my question:
Depending on which url i use (or how i reached the page) i want ether menu item"projects" or "blog" to be active...

i thied modifiing navigation.html.twig, but i guess thats not the right way(?)
i serched the forums but couldn't find the answer

Thanks in advance!

9 years ago

I think modifying navigation.html.twig is the right way. However such modification cause difficulty when the theme is updated and without caution you loose your modifications.

A solution is to use theme inheritance. In 2014 rhukster has written a blog post about theme inheritance.

I hope this helps.

9 years ago Solution

Thanks for your answer!
I'm already using theme inheritance, forgot to mention.

problem solved for me:
in 'navigation.html.twig' i replaced

{% set active_page = (p.active or p.activeChild) ? 'active' : '' %}

with

{% set active_page = p.slug == (grav.uri.route|split('/')[1]) ? 'active' : '' %}

it took a while until i found 'grav.uri.route' but now it works perfectly!

Suggested topics

Topic Participants Replies Views Activity
Themes & Styling · by MarkP, 2 weeks ago
3 193 2 weeks ago
Themes & Styling · by TomW, 2 weeks ago
0 111 2 weeks ago
Themes & Styling · by themetman, 3 weeks ago
1 206 3 weeks ago
Themes & Styling · by AH, 1 month ago
1 324 3 weeks ago
Themes & Styling · by Bruno, 4 years ago
6 1523 3 weeks ago