I've searched and found variations of this question but I've never found an answer that seemed to work. I need to add a class to the <li> elements, currently the main menu is rendered like this:

print theme('links__system_main_menu', array(
    'links' => $main_menu,
    'attributes' => array(
        'id' => 'slidemenu',
        'class' => array(
    'heading' => t('')

As far as I can tell, this only sets up the <ul> element of the main menu, so I was wondering if there's a way to edit the attributes of the 'links' part.


Alright, I think I solved it. Apparently you can use theme_links__system_main_menu in templates to override the whole thing. I ended up doing this:

function mytheme_links__system_main_menu($variables) {
    $html = '<div id="mainmenu">';
    $html .= '  <ul id="slidemenu" class="clearfix">';

    static $linkClass = 1;

    foreach ($variables['links'] as $link) {                
        $html .= '<li class="themastyle' . $linkClass . '">' . l($link['title'], $link['href'], $link) . '</li>';

    $html .= '  </ul>';
    $html .= '</div>';

    return $html;

Not entirely sure if this is correct Drupal, though it did solve my issue!

  • 1
    Welcome to Drupal Answers. Which version of Drupal? You can tag your question "6" for Drupal 6 or "7" for Drupal 7. Also, it may help if you could link to the answers you tried that didn't work. Apr 4 '12 at 7:13
  • Most of the other questions I've seen were about adding IDs and classes to list items of menus (drupal.org/node/1201248). Though it would seem the main menu and block menus are largely separate affairs. I've managed to add classes to block menus with theme_menu_link, though they don't apply to the main menu
    – Zedicius
    Apr 4 '12 at 7:29
  • Your solution after the edit is not a correct Drupal solution. You should rather look at this answer with some links to other solutions: stackoverflow.com/a/5200802/517705. By the way, if you think you solved your own question, you should create a separate answer, and accept your own's! Thanks!
    – Sk8erPeter
    Apr 4 '12 at 17:13

print_r($main_menu); each $main_menu items should have this structure:

    'title' => 'xxx',
    'href' => 'xxx',
    'attributes' => array(
        'class' => array('xxx'),

so you can add class to li by altering $main_menu array.

  • This might be what I'm looking for. Could someone elaborate on how to edit $main_menu ?
    – Zedicius
    Apr 4 '12 at 7:30
  • in your question you mention main menu is rendered like ..., before this line of code you can modify your $main_menu array. Apr 4 '12 at 7:34

Menu Attributes is another great module that allows you to set class, id, rel and other attributes on either the a or li element.

  • Yes, in my experience Menu Attributes is the best choice for most projects. And I'm surprised this answer doesn't have more upvotes, with almost 6k views on this question.
    – kqw
    Nov 4 '15 at 11:16

if your looking for a D8+ fix, the Menu Link Attributes module saves a lot of time. You can specify attributes and classes through a YML file or directly to individual links in Admin Menu UI.


If you don't mind using a existing module, you could also use Menu Names CSS. This module adds the target path of a menu item as a sanitized css-class to the li-item.

A second benefit is, that the module is good template how to put your functionality in a module instead of the template.php file, to increase the re-useability of your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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