How to fetch submenu items for current page? My site nav is all in a single menu: main-menu, eg:


Home
About

Director Name 1
Director Name 2

Contact


My site navigation bar comprises two horizontal strips: the upper one lists all top-level pages (ie: Home, About, Contact, Services). The lower one should be populated with the revelant sub-menu items for the current page. So if you're on the Home page, the bar will be empty. If you're on the About page, the bar will contain links to each of the Directors pages.

My top menu is populated by the following, unchanged from the code in the default page.tpl.php:

&lt;?php print theme('links__system_main_menu', array('links' =&gt; $main_menu, 'attributes' =&gt; array('id' =&gt; 'main-menu', 'class' =&gt; array('links', 'inline', 'clearfix')))); ?&gt;


To get the sub-menu items, however, I've had to add the following to my theme's template.php:

function mytheme_preprocess_page(&amp;$vars){

    $vars['page_submenu'] = NULL;
    $main_menu_tree = menu_tree_page_data('main-menu', NULL, TRUE);
    foreach ($main_menu_tree as $menu_item) {
        if (count($menu_item['below']) &gt; 0) {
            $submenu = $menu_item['below'];
        }
    }
    if ( !empty($submenu)){
        $vars['page_submenu'] = menu_tree_output($submenu);
    }
}


... basically, "get the main menu, find the menu item in it that has some "below" items, and return that "below" bit as a menu tree".

I can then just render that in the template:

print drupal_render($page_submenu);


This seems a bit kludgy. Returning only the submenu items for the current page would seem like a fairly common requirement - is there a better way to do it?
You are right it's a pretty common requirement. If you are not afraid of slight performance hit, you may use Menu Block module. It is able to provide you with submenu relative to your current path:

So if you’re only using your theme’s Main menu links feature, you can add and configure a “Main menu (levels 2+)” block. That block would appear once you were on one of the Main menu’s pages and would show the menu tree for the 2nd level (and deeper) of your Main menu and would expand as you traversed down the tree. You can also limit the depth of the menu’s tree (e.g. “Main menu (levels 2-3)”) and/or expand all the child sub-menus (e.g. “Main menu (expanded levels 2+)”).
Pretty simple, eh?

Of course it is a pretty big module, and it puts additional block layout between your menu and your display, so if it is too much of a performance hit, you can try to borrow some of it's code - it's GPL after all.