0

I am theming my website and used the following code to print my main menu.

<?php $main_menu = variable_get('menu_main_links_source', 'main-menu'); $tree = menu_tree($main_menu); print render($tree); ?> 

Now, I need to have individual class name of each list item, so that I can call different background images using css. I would like to have class with name 'menu-497' as shown in screenshot. I couldn't figure out how this is done. Please help. enter image description here
Old code which used to load individual list class is below. But this code won't load submenu, so I am not using it.

<?php print theme('links__system_main_menu', array( 'links' => $main_menu, 'attributes' => array( 'id' => 'topNav', ), )); ?> 

1 Answer 1

1

It looks like you need Menu attributes module:

You should use this module when

  • You want to "nofollow" certain menu items to sculpt the flow of PageRank through your site
  • You want to give a menu item an ID so you can easily select it using jQuery
  • You want to add additional classes or styles to a menu item

Alternatively, if you don't mind some coding, see theme_menu_link(). Comments in it's documentation already shows how to do it:

 function superseven_menu_link(array $variables) { $variables['element']['#attributes']['class'][] = 'menu-' . $variables['element']['#original_link']['mlid']; return theme_menu_link($variables); } 
5
  • Yes, thank you Molot. But I couldn't understand what I have missed in above code, so that I am not getting individual class with number in each list items. I am in a position where, I couldn't change my code. Please help. Commented Mar 31, 2014 at 8:58
  • @SureshAlagar You shouldn't really try to add dynamic classes at tpl level, that's the problem (or at least one of them). Also, I can't see which line of code in your question was supposed to generate these classes at all. Commented Mar 31, 2014 at 9:00
  • The code which use to generate class (like class='menu-497') for each list item is added in question. But it had a problem that it won't load up sub menu. (old code added in question) So, I used another code(show in question) to solve my problem. It load sub-menu but it had class name missing for each list items. Commented Mar 31, 2014 at 9:25
  • @SureshAlagar please use edit option on your original question - this site is not a support forum. But if your question will be chameleon question, I'll just remove my answer and will avoid your questions until I'll forget it ;) So please don't be a chameleon. Commented Mar 31, 2014 at 9:29
  • I am sorry for this. I've modified my question. Commented Mar 31, 2014 at 9:33

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.