Enable WordPress 3.0 custom menu in your theme

wordpress-custom-menu

With the release of WordPress 3 the users can now activate the new custom menu feature which let’s you easily customize menus. Custom Navigation Menus feature in WordPress 3.0 will make WordPress even more user friendly for beginners. This function let you organize your menu, create drop down menus, add new items to menu, and much more. The drag-and-drop function of this feature is what makes it extremely easy to use. This feature will not be available in older themes unless the theme author(s) update their themes. In this article, we will show you how you can enable and install custom navigation menus in your themes.

Check if the theme has custom menus enabled

To see if your theme has the new custom menus feature enabled, while in the Admin area, go to Appearence > Menus. If your current theme doesn’t support custom menus you’ll see this error message:

custom-menu-error

Enable the custom menu feature for your theme

Now, to enable the feature for your theme go to functions.php file and enter the following code:

<?php
if ( function_exists( 'register_nav_menu' ) ) {
    add_action( 'init', 'register_my_menu' );
    function register_my_menu() {
	register_nav_menu( 'primary-menu', __( 'Primary Menu' ) );
    }
}
?>

The above code does the following:

  • Check to see if the register_nav_menu function exists
  • If the function exists, after WordPress has finished loading but before any headers are sent, call the register_my_menu function
  • Register a new menu with the name of Primary Menu

Display the new menu in your theme

To display the newly created menu enter the following code in the header.php file or where ever you want to display the menu

<?php
$args = array('theme_location' => 'primary-menu');
wp_nav_menu($args);
?>

Now your theme has the new custom menu feature enabled. To add/remove/modify the menu you can now login to WP-ADMIN and go to Appearence > Menus

Customizing the menu structure using the wp_nav_menu arguments

If you want your menu to have a custom structure/class/id you can use the wp_nav_menu parameters. Bellow is a list with the parameters and their default values that the function accepts:

<?php
$args = array(
  'theme_location'  => ,
  'menu'            => ,
  'container'       => 'div',
  'container_class' => 'menu-{menu slug}-container',
  'container_id'    => ,
  'menu_class'      => 'menu',
  'menu_id'         => ,
  'echo'            => true,
  'fallback_cb'     => 'wp_page_menu',
  'before'          => ,
  'after'           => ,
  'link_before'     => ,
  'link_after'      => ,
  'items_wrap'      => '
    %3$s
', 'depth' => 0, 'walker' => ); ?>

For a detailed list about each parameter check the WordPress Codex Page

 

Comments

Post Comment
  • The post has no comments. Be the first one to comment

Add Comment

Blog Categories

Popular Posts