Community Documentation

Advanced theming with functions

Last updated October 21, 2010. Created by add1sun on March 31, 2008.
Edited by tim.plunkett, domesticat. Log in to edit this page.

NOTE: These examples are taken from the 1.x version of Nice Menus. The function names and some contents have changed in the 2.x version, so modify accordingly. All theme function names in 2.x start with theme_nice_menus (with the "s") and in 1.x they don't have the "s."

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_menu. 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.

Call a function directly in your theme's template file

Just print (or echo) this line:

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

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_menu_primary_links');
}
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', $primary_links);
}
?>

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_menu_primary_links($direction = 'down', $menu = NULL) {
 
$pid = variable_get('menu_primary_menu', 0);
 
$output = theme('nice_menu', 'primary', $pid, $direction, $menu);
 
// 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);
  }
}
?>

Comments

Hello, I add the following

Hello,
I add the following code to my page.tpl.php:

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

But the Primary Menu isnt't display.
I use drupal7.
What can I do?

Drupal 7 Changes

primary-links isn't what the main menu is called in Drupal 7, which is what the current Nice Menus D7 beta still uses. It's main-menu.

Use the following instead:

<?php
 
// id, direction, depth should have the values you want them to have.
 
$menu = theme('nice_menus', array('id' => 0, 'direction' => 'down', 'depth' => 1, 'menu_name' => 'main-menu', 'menu' => NULL));
  print
$menu['content'];
?>

For Drupal 7 main menu you

For Drupal 7 main menu you can also do this:

1. Make sure your page.tpl.php returns the main menu variable somewhere:

<?php
print $main_menu;
?>

2. In template.php in your hook_preprocess_page function, place the following lines:

<?php
 
if (isset($variables['main_menu'])) {
     
$variables['main_menu'] = theme('nice_menus_main_menu');
  }
?>

thanks

thanks mradcliff for this and its working only for sub menu..but its not working in super sub menu like...
menu->sub menu->super sub menu
i want to add like this
main menu-category
sub menu-php
super sub menu-cake php, coreph and advanced php
its shows in menu drop down .
any module we have in drupal7..
iam new to drupal thats way please post solution this....

Depth

Please review the API for theme_nice_menus(). There is a depth argument.

thanks,please give me

thanks,please give me clearly....i am not understanding...

Poor documentation

I think you guys assume that all people are Drupal expert already but it's unfortunately not.

You never say this call is used for which versions:

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

And while you are explaining this function
theme_nice_menu($id, $menu_name, $mlid, $direction = 'right', $menu = NULL)

You are actually giving an example of
$menu = theme('nice_menu', 1, 'navigation', 17, 'down');
print $menu['content'];

which really confuse people. I know they are something relative but come on, please make it more logically documented.

Thanks.

Video Getting Nice Menus to Work

Here's a down a dirty video I recorded that gets this to work. I'll try to record something better in future.

http://www.youtube.com/watch?v=oPP6LNLeGcg

Is not working with third levels?

EDIT: I solved the problem I was having no rendering the sub levels in the drop-down menu as primary menus, I simply use the hint in the code called above, which somewhat i have missed that line:

<?php
 
// id, direction, depth should have the values you want them to have.
 
$menu = theme('nice_menus', array('id' => 0, 'direction' => 'down', 'depth' => 1, 'menu_name' => 'main-menu', 'menu' => NULL));
  print
$menu['content'];
?>

thanks and thanks for the video which was also very handy.

Excelent, but for me new problem :(

I finale made nice menus to works thanks for the help, but i might have done something wrong, because now my butons are not round any more, they are just squares :( is it suposed to hapen, sorry about the question but i am quite newbie in drupal
All the Best

Ok problem solved, some peeking solved the problem :)

Same Issue with me

would you please specify the "peeking" :) as i'm facing the same problem :"now my butons are not round any more, they are just squares".
thanks in advance

nobody click here