Menu theming

Last updated on
31 August 2016

The new theme_links($variables) function in D7 receives only an associative array to build the entire link structure. As you can read in the documentation, your links should be passed through the links argument as an associative array, but does not describe where and/or how you get this array.

The following is an example of how to do it with the user menu:

<?php
$user_menu = menu_navigation_links('user-menu'); 
print theme('links', array(
  'links' => $user_menu,
  'attributes' => array(
    'id' => 'user-menu',
    'class' => array('links', 'clearfix'),
  ),
  'heading' => array(
    'text' => t('User menu'),
    'level' => 'h2',
    'class' => array('element-invisible'),
  ),
));
?>

If you are wondering what the "user-menu" argument is in the menu_navigation_links($menu_name, $level = 0) function, it is the name of the menu to be printed. You can find the menu name for this example on the User menu administration page (Administration > Structure > Menus > User menu or http://example.com/admin/structure/menu/manage/user-menu/edit) on your site.

In this example, the $user_menu variable is not mandatory, but doing it this way, you can later use it in a conditional statement that is quite common in the templates.