6

I have a site that has a lot of menus. One of them is the main menu that has few items. These items link to main areas of the site, and each of these main areas has one specific menu. When I'm on a landing page of one of these main areas, the item of it on the main menu is active, but when I'm on an internal page of one of these areas, the item on the main menu is not active.

Is there any way to make it active automatically?

I tried using menu_position but it seems not be working.

5 Answers 5

11

Context provides this functionality out of the box.

14

Their is a lot of ways. I list here only things compatible with D7 and eventually on D6.

Use a modules

Code your stuff

Their is now a native API menu_tree_set_path to set the path you want to use to calculate the menu active trail.

You can also work in theme_links but but I recommend to use menu_tree_set_path so other module know what is your active link. The advantage is you can use this function in your theme without creating a module.

Keep in mind, menu_position has an API to create your custom rules.

So what should I use???

It's depend of your need. if you have simple rules like: for this node type/views/taxonomy i want this active menu, context is good.

If you have a good url structure (autopath) than follow your menu structure, menu_trail_by_path will just works without any effort.

I never used menu_position... so I can very tell.

If you need some special logic, coding your things is certainly faster than creating 40 context or rules...

4

The easiest way is just to make those menu items of your "internal" pages (subpages belonging to your areas/landing pages) children of landing pages menu items.

Something like:

primary links menu
|
+-- landing page 1 menu item
|   |
|   +-- internal page 1 menu item
|   +-- internal page 2 menu item
|
+-- landing page 2 menu item
    |
    +-- internal page 3 menu item
    +-- internal page 4 menu item

If you have to keep them in separate menus though, you would probably need to play with theme_links() to keep proper "active-trail" and/or "active" classes.

2

Perhaps the Menu Block module would help you.

(In conjunction with the approach that Maciej outlines above).

From the Menu Block project page:

It provides configurable blocks of menu trees starting with any level of any menu. And more!

So if you’re only using your theme’s Main menu links feature, you can add and configure a “Main menu (levels 2+)” block. That block would appear once you were on one of the Main menu’s pages and would show the menu tree for the 2nd level (and deeper) of your Main menu and would expand as you traversed down the tree. You can also limit the depth of the menu’s tree (e.g. “Main menu (levels 2-3)”) and/or expand all the child sub-menus (e.g. “Main menu (expanded levels 2+)”).

1
  • I tried Menu Block but it didn't worked as expected Commented Sep 16, 2011 at 16:30
1

You can also use hook_translated_menu_link_alter(). This would be little tricky.

/**
 * Implements hook_translated_menu_link_alter()
 */
function Module_translated_menu_link_alter(&$item, $map) {

    if ($item['menu_name'] == 'menu-name') {

        //check first two arg from url
        $path = arg(0)."/".arg(1);

        //add class active-trail if path match
        if (strpos($item['link_path'], $path) !== false) {
            $item['in_active_trail'] = true;

        }

    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.