Drupal 6 & 7 uses version 2 of Nice Menus . These have changed in version 2.x. Theme functions can be called using nice_menus, with the "s"on the end.

theme_nice_menu($id, $menu_name, $mlid, $direction = 'right', $menu = NULL)

$id
The nice menu ID. You can find the ID for a particular block you have created by going to Administer > Structure > Blocks (example.com/admin/structure/block) and hovering over the "configure" link (e.g. example.com/admin/structure/block/manage/nice_menus/1/configure). The $id for the theme function is the number just before '/configure'.

$menu_name
The top parent menu name from which to build the full menu. You can find a menu name by going to Administer > Structure > Menus and clicking to view the list of menu items. The URL for that page contains the menu name (e.g. example.com/admin/structure/menu/manage/menu-custom). In this example, the $menu_name would be 'menu-custom'.

$mlid
The menu ID from which to build the displayed menu. If you use NULL here, then the whole menu will be displayed. If you only want a subset from the menu, then you can enter the menu id for the top item to use. You can find the menu id for any menu item by going to Administer > Site Building > Menus, click the menu name of the list you want to see, then hover over the edit link for the item (e.g. example.com/admin/structure/menu/manage/menu-custom/edit). The $mlid for this item would be the string or number just before '/edit' - in this example, menu-custom.

$direction
Optional. The direction the menu expands. Default is 'right'. Other options are 'down' and 'left'.

$menu
Optional. A custom menu array to use for theming -- it should have the same structure as that returned by menu_tree_all_data(). Default is the standard menu tree.

Calling the Function

Will print out the navigation menu (Drupal core's admin menu) but only starting with menu item # 17, which is the Site Building item, using dropdowns. The menu will be printed out in the 'content' region of the theme.

$menu = theme('nice_menu', 1, 'navigation', 17, 'down');
print $menu['content'];

Will print out the entire menu named "menu-custom" flying out to the left.

$menu = theme('nice_menu', 1, 'menu-custom', NULL, 'left');
print $menu['content'];

Helper functions for main/secondary menus.

There are also helper functions which provide wrappers around theme_nice_menu() to make it easier to display those in your theme.

This will print out the main menu:
print theme('nice_menus_main_menu');

This will print out the secondary menu:
print theme('nice_menus_secondary_menu');

Comments

koorneef’s picture

Since D6 the book hierarchy is encorporated in the menu tables. This means that it is now very easy to use a nice menu for book navigation.

Here is an example I'm currently using in page.tpl.php:

  $menu = theme('nice_menu', 1, 'book-toc-1', NULL, 'down');
  print $menu['content'];

'book-toc-1' is the name of the menu (the node with node id 1 is the parent node in the book).

This is very handy if your site has lot of static nodes as book pages. Using the method above means that you don't have to keep the menu 'tree' and the book 'tree' in sync by hand.

Note:
Mixing a book 'menu' with other menu items will not work. The menu will only display all the nodes in the book structure.

Please contact me if I've overlooked something or you have other remarks.

PS: I'm aware of: http://drupal.org/node/134510
But the code listed on that page will not work for D6. And the solution above is a bit more elegant or not ?

Mark B’s picture

I'm building a pretty big site with several books, so use this custom block code to create a nicemenu for all the books on the site:

<?php
  $books = book_get_books();
  foreach($books as $book_nid=>$book) {
    $menu = theme('nice_menu', $book_nid, $book['menu_name'], NULL, 'down');
    $master_menu .= $menu['content'];    
  }
  print $master_menu;
?>
Diegen’s picture

Thanks for this snippet !! Life saver, time saver

bradfarris’s picture

Hi. I notice that you describe a function with the signature "theme_nice_menu($id, $menu_name, $mlid, $direction = 'right', $menu = NULL)", but that the examples seem to use the "theme()" function. Would it be possible to provide an example which uses the "theme_nice_menu()" function?

jdelgama’s picture

Just change the function by the newer one. And look: you have to put an 's' in the name of the function to work! theme_nice_menus

<?php
  $books = book_get_books();
  foreach($books as $book_nid=>$book) {
    $menu = theme_nice_menus($book_nid, $book['menu_name'], NULL, 'right');
    $master_menu .= $menu['content']; 
  }
  print $master_menu;
?>
nicknickoli’s picture

I couldn't find anything about how to do this and there may be better functions to get parent-menu-item ids. This works for Drupal 6 and nice_menus 2, showing the menu tree for some depth below the fist item.

function MYTHEME_nice_menus($id = null, $menu_name = null, $mlid = null, $direction = 'right', $depth = '-1', $menu = NULL)
{
if($id == 2) 
{ // my special menu 
  if(($parent_menu_item = _MYTHEME_fetch_menu_parent()) && $parent_menu_item->mlid)  
  {
$menu_tree = theme('nice_menus_tree', $menu_name, $parent_menu_item->mlid, $depth, $menu);
$output['content'] = '<ul>' . $menu_tree['content'] . '</ul>';
$output['subject'] = $parent_menu_item->link_title;

// else copy the theme default from nice_menus.module 




function _MYTHEME_fetch_menu_parent($link_path = null, $starting_depth = 0)
{
	$link_path = $link_path ? $link_path : implode('/', arg());
	$menu_items[] = db_fetch_object(db_query('SELECT * FROM menu_links where menu_name="%s" AND link_path="%s" LIMIT 1', 'primary-links', $link_path));
	while(count($menu_items) > 0 && $menu_items[count($menu_items)-1]->plid) {
		$q = db_query('SELECT * FROM menu_links where menu_name="%s" AND mlid="%s" LIMIT 1', 'primary-links', $menu_items[count($menu_items)-1]->plid);
		$menu_items[] = db_fetch_object($q);
	}
	$menu_items = array_reverse($menu_items);
	return empty($menu_items[ $starting_depth ]) ? false : $menu_items[ $starting_depth ]; 
} 
tinto’s picture

I've been trying to create a nice menu block that displays only child menu items of the parent selected, but no luck so far. I think this code does exactly what I need, but as far as I can tell, it only works in D6.

Has anyone managed to get this code to work in Drupal 7 / Nice Menus 2.x?

Sinan Erdem’s picture

Subscribe

Ayesh’s picture

$master_menu = '';
$books = book_get_books();
foreach($books as $book_nid=>$book) {
  $menu = theme('nice_menus', array('id' => $book['mlid'], 'direction' => 'right', 'depth' => -1, 'menu_name' => $book['menu_name'], 'menu' => NULL));
  $master_menu .= $menu['content'];
}
print $master_menu;

thanks to jdelgama for original code!

xaa’s picture

many thanks Ayesh !

xaa’s picture

hi, is it a way to order the menu-item with your snippet ?
rgs,

gambry’s picture

Needing to print my custom menu as a nice menu, I tried all the theme functions proposed in this documentation without any results, so I want to leave here the only one working (for me? or at all?) on Drupal 7.x NiceMenu 2.1:

$temporary = theme_nice_menus(array(@params));
print $temporary['content'];

in my case I've used:

$temporary = theme_nice_menus(array('id'=>0,'menu_name' => 'my-custom-menu-name','direction' => 'down', 'depth' => -1));
print $temporary['content'];

My 2 cents.