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

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Themes & Styling

Pages in multiple categories -> active menu item

Solved by johannes t View solution

Started by johannes t 8 years ago · 2 replies · 1081 views
8 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!

8 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.

8 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 Pedro M, 2 months ago
4 193 2 months ago
Themes & Styling · by Ian, 2 months ago
3 90 2 months ago
Themes & Styling · by Norbert, 2 years ago
11 449 3 months ago
Themes & Styling · by Lukáš Findeis, 3 months ago
0 44 3 months ago
Themes & Styling · by Sebadamus, 4 months ago
5 123 3 months ago