Advanced theming with functions

The Nice menus module comes with several theme functions which you can use directly. This lets you have greater control over the HTML and avoids having the additional code that wraps a Nice menu when it is generated by the block system. This page will provide a brief overview on how to use a theme function directly, but you should refer to the Theming Guide for your version of Drupal for more detailed information on using theme functions. Details about the Nice menus functions themselves follow in the next sections, identified by version.

All theme functions can be either called directly, as is, from Nice menus or you can override them in your theme. They are all located in the nice_menus.module file and begin with theme_nice_menus. These examples below are only to show the method and show the simplest form of the function. For details on the parameters you can/should use, please see the version-specific documentation that follows.

Note: information has been updated to reflect the 7.x-2.x branch of Nice Menus.

Call a function directly in your theme's template file

Just print (or echo) this line:

<?php print theme('nice_menus_main_menu'); ?>

Or use the following version, which tests to see if Nice Menus is enabled before rendering the links:

<?php
if (module_exists('nice_menus')) {
  print theme('nice_menus_main_menu');
}
else {
  // Note: the exact syntax of this line may change,
  // depending on your theme. Look at how your theme
  // prints this line, and edit it accordingly.
  print theme('links', $main_menu);
}
?>

The longer version is useful when there is a possibility that Nice Menus may be disabled or uninstalled, such as in multi-site installs, or during upgrades.

To override (change) the output

To change the look of the output, copy the entire function from nice_menus.module into your theme's template.php and rename the file to match your theme.

<?php
function THEMENAME_nice_menus_main_menu($variables) {
  $direction = $variables['direction'];
  $depth = $variables['depth'];
  $menu_name = variable_get('menu_main_links_source', 'main-menu');
  $output = theme('nice_menus', array('id' => 0, 'menu_name' => $menu_name, 'mlid' => 0, 'direction' => $direction, 'depth' => $depth));
  // Adding this silly line just to show that you can change the output of the function.
  $output['content'] .= t('I can put anything I want in this function.');
  return $output['content'];
}
?>

To exclude certain items from Nice Menus

Add the following code to your theme's template.php and rename the file to match your theme:

<?php
function THEMENAME_nice_menus_tree($pid = 1, $menu = NULL) {
  // For multiple items, edit the next line like this:
  // $menu_item_to_ignore = array(123, 456, 789);
  $menu_item_to_ignore = array(123);
  if (in_array($pid, $menu_item_to_ignore)) {
    return '';
  }
  else {
    return theme_nice_menus_tree($pid, $menu);
  }
}
?>

custom nice_menus in theme:

Drupal 6 nice_menus-6.x

Use theme:

<?php
$nice_menus = theme('nice_menus', 0 /* id */,
    //menu-{menu_name}, use dpm(menu_get_menus()) see menu name.
    'menu-beer',
    // mlid: The menu ID from which to start building the items, i.e. the parent of the displayed menu.
    0,
    //'right' 'left' 'down'.
    'down',
    // depth: The number of children levels to display. Use -1 to display all children
    // and use 0 to display no children.
    '-1',
    // Menu data. default is NULL.
    NULL);
print($nice_menus['content']);
?>

use block theme:

<?php
$nice_menus_block = (object) module_invoke('nice_menus', 'block', 'view', "2");
print theme('block',$nice_menus_block);
?>

Drupal 7 nice_menus 7-x

Use theme:

<?php
$nice_menus = theme('nice_menus', array(
 'id' => 100,
 'menu_name' => 'menu-beer', //menu-{menu_name}, use dpm(menu_get_menus()) see menu name.

 // The menu ID from which to start building the items, i.e. the parent
 // of the displayed menu.
 'mlid' => 0,

 'direction' => 'down',  //'right' 'left' 'down'

 // The number of children levels to display. Use -1 to display all children
 // and use 0 to display no children.
 'depth' => '-1',

  // "Show as expanded" option.
 'respect_expanded' => FALSE
));
print($nice_menus['content']);
?>


Use block:

<?php
// Load the block region based on module and delta key.   
$nice_menus_block = _block_get_renderable_array(_block_render_blocks(array(block_load('nice_menus', '2' /*1,2,3 more,  this is get nice_menus_2 block*/))));
print render($nice_menus_block);
?>

NiceMenus Theme Functions in 7.x & 6.x

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

NiceMenus Theme functions in 5.x

Question

Primary links as nice menus

This post is in the issue queue related to the Nice_menus module. It could be a help to a lot of people.Basically you can use the theme_nice

Guide maintainers

add1sun's picture