Drupal: Adding a tertiary menu to your theme

Unfortunately Drupal only allows you to add primary and secondary menu’s by default. However there are times you need deeper levels, for example a third (tertiary) level submenu.
After some searching and looking at the core code I came up with a pretty easy solution.
First add the following functions to your template.php file.

function phptemplate_preprocess_page(&$vars) {
$vars['tertiary_links'] = menu_tertiary_links();
}

/**
* Returns an array containing the tertiary links based on the primary menu.
* Tertiary links can be either a third level of the Primary links
* menu or generated from a second level of the explicitly defined secondary_menu
*/
function menu_tertiary_links() {
if (variable_get('menu_secondary_links_source', 'secondary-links') == variable_get('menu_primary_links_source', 'primary-links')) {
return menu_navigation_links(variable_get('menu_primary_links_source', 'primary-links'), 2);
}
else {
return menu_navigation_links(variable_get('menu_secondary_links_source', 'secondary-links'), 3);
}
}

Once you’ve done that, you can add this snippet in your template.

<?php if (isset($tertiary_links)): ?>
<?php print theme('links', $tertiary_links, array('class' => 'links tertiary-links')); ?>
<?php endif; ?>

And voila! you’re done.