As you know it is possible to set the source for the secondary links to be primary links. This way any subitems of the active primary link will show up in the secondary links.
So I set up my primary links to be a menu tree like this:
- Primary links
- Section one
- item 1
- item 2
- ...
- Section two
- item 3
- item 4
- ...
- Section one
Now whenever I click "Section one" in the primary links, "item 1" and "item 2" shows up in my secondary, and "Section one" gets a css class that marks it as active. Fine and dandy just like I want it.
BUT, as soon as I click any item in the secondary links the parent item in primary links is no longer marked active.
The theme uses these statements to print the links:
print theme('links', $primary_links, array('class' => 'links primary-links'));
print theme('links', $secondary_links, array('class' => 'links secondary-links'));
What I would like to is to have the links themed more like the regular menu tree where I get the class "active-trail" on parent items of the currently active item but I don't want them as a tree. In other words, I still want to use the method where primary and secondary links get printed in separate lists, but I also want to have the "active-trail" on the primary link when a child is active.
I hope I have made myself clear enough :)
thanks
Ludde
Comments
Assuming I'm understanding
Assuming I'm understanding what you're trying to accomplish this should be pretty easy with a theme_ function. Try overriding the theme_menu_item() function in your template.php file with something like this.
Just put that in your theme's template.php file, and change the function name to _menu_item and you "should" be good to go. This will ad a class of "active-trail" to all li items that are a parent of the currently active menu item.
Thanks for your help,
Thanks for your help, although I don't think you understood what I'm trying to accomplish. And drupal 6.x already works like that. If I display the primary links as a menu-block I'm getting the active-trail without overiding or hacking anything.
My problem is that I can't use that kind of menu. I need the top level items in one list and the second level items in another list, and I can't have them nested.
The primary and secondary links are themed by the function theme_links(...), which I can override if I place a function mythemename_links(...) in template.php, but as far as I can see there is no way of knowing if a certain item is supposed to be regarded as the active trail.
Have I made myself clearer? Or did I maybe misunderstand you?
thanks
Ludde
Found the solution
After digging into the core api functions I found that this is actually already implemented in the core, but it's not included in 6.2.
The function on this page differs slightly from the one shipped with 6.2.
http://api.drupal.org/api/function/menu_navigation_links/6
this is what it looks like on the api documentation page:
and this is the chunk that was added since 6.2
There is however a problem with this, since it will overwrite $l['attributes'] without checking if it already exists. In my case the title attribute for the link disappeared.
To get rid of that problem I changed it to look like this and it seems to work perfectly:
cheers
Ludde
Hopefully there is another way
You really don't want to hack core files (although I do encourage you to submit this as a patch).
There is a way to do this using theme overrides, but I don't have an installation of D6 to experiment with. I can tell you what to do, though:
start by overriding theme_links(). Call menu_get_active_trail() to see what all pages are in the "active trail". Compare the contents of menu_get_active_trail with the link being themed, and if they match, then add the "active-trail" class to the html.
You may need to do a var_dump() to see how to approach whatever was returned by menu_get_active_trail.
Sorry I couldn't be more helpful.
- Corey
Hi, I have submitted an
Hi,
I have submitted an issue and the code to fix it: #273129: Adding class active-trail in menu_navigation_links(...) removes previous attributes
I'll have a look at menu_get_active_trail() again to see if I can use that instead of changing the core.
thanks
/Ludde
Set parent item active
i didn't get access to theme_links() probably this function is discontinued.
i acomplished it overriding theme_menu_item_link()
hope this can help people with the same issue
Miguel
www.drupaltutorials.net