Tuesday, May 28, 2013

Set menu item active using custom code.

If you want to set a menu item of the main menu as active (class="active"). Here is how you can do this..
function mymodule_init() {
    $link=menu_link_load(2570);
    $link["options"]["attributes"]["class"]="active";
    menu_link_save($link);
}
2570 is mlid of that perticuler menu item. You can check it by editing that perticuler menu item "admin/build/menu/item/2570/edit" Now, I just want to set it active on some specific pages... like "mypage01", "mypage02" etc
function mymodule_init() {
  if((arg(0) == 'mypage01') || (arg(0) == 'mypage02')) {
    $link=menu_link_load(2570);
    $link["options"]["attributes"]["class"]="active";
    menu_link_save($link);
    $menu_active = 1;
  }
  else {
   if($menu_active == 1) {
     $link=menu_link_load(2570);
     $link["options"]["attributes"]["class"]="";
     menu_link_save($link);
     $menu_active = 0;
   }
  }
}
There you go...

No comments:

Post a Comment