How to customise theme_menu_tree

2 replies [Last post]
fireforce
Offline
Last seen: 8 years 43 weeks ago
Joined: 2012-03-31

I'm working on a navigation menu. I output the Main menu in the page.tpl.php with the following code in template.php:

function mytheme_preprocess_page(&$vars, $hook) {
 
 $main_menu = menu_tree_all_data('main-menu');
    $vars['main_menu_expanded'] = menu_tree_output($main_menu);  

.......

It output the tree menu perfectly. Now, I wish to add an ID and new Class attributes to the Main menu so that it will be output like:

 <ul id="my_id" class="my_new_class">
   <li><a href="">Menu1</a>
     <ul>
       <li><a href="">submenu</a></li>
     </ul>
   </li>  
   <li><a href="">Menu2</a></li>
 </ul>

So, I tried to add the following code:

  function mytheme_menu_tree($variables) {
    return '<ul id="my_id" class="my_new_class">' . $variables['tree'] . '</ul>';
  }.......

But this actually applied to all the menus and all the ul tags (include the submenu's ul tags). How can I:

1) Add attributes only to a particular menu, in my case, Main menu
2) The ID and class added only to the parent menu (First level), not the submenu's ul.

Or any other better way to do this. Please advise. Thanks!

fireforce
Offline
Last seen: 8 years 43 weeks ago
Joined: 2012-03-31
I found the way of doing

I found the way of doing it...

function mytheme_menu_tree__main_menu($variables) {
    return '<ul id="my_id" class="my_new_class">' . $variables['tree'] . '</ul>';
  }.......

REF: http://drupal.org/node/254940#theme-suggestions-for-menus

Now this only applied to the main menu without affecting others. However, all the ul tags within this menu including the submenu has been given the same id and class as shown below:

<ul id="my_id" class="my_new_class">
   <li><a href="">Menu1</a>
     <ul id="my_id" class="my_new_class">
       <li><a href="">submenu</a></li>
     </ul>
   </li>  
   <li><a href="">Menu2</a></li>
 </ul>

I wonder how to narrow it down only apply to the parent menu (level 1), not it's submenu. Just 1 step missing, please help. Thanks!

fireforce
Offline
Last seen: 8 years 43 weeks ago
Joined: 2012-03-31
any idea or suggestion?

any idea or suggestion?

Add to playlist

Add to , or

Add to new playlist:

Add to playlist
This is a member-only feature
But, it's easy to become a member

Add to cart:


Loading...
Contact us

Below are the supported keyboard shortcuts for our video player.

space
Start / stop video
shift
Go to next video
shift
Go to previous video
Increase or decrease player speed by 20%
Skip ahead or go back by 15 seconds
1...9
Jump to a percentage (2 is 20%, for example)
f
Toggle full screen
Videos Pricing More Search
BuildAModule Logo Icon BuildAModule Logo Icon
Become a member!
$29
1 month
$156
6 months
10% off
$276
12 months
20% off
All memberships come with the following:
24 / 7 access to the entire 1800+ video library
An innovative video player with a rolling transcript
Extensive Drupal and cross-platform training
A thorough and thoughtful training style
Progress tracking
Take notes on specific video sections
Intuitive multi-user management
How many users?
user
Go solo with a single user, or give multiple team members access through one account and get discounts by bundling multiple users into a single membership.