Custom Menus in WP 3.0072010
Jul
Jul
There are lots of great new features in WordPress 3.0, one of my favs is the ability to create custom menus through the interface. The custom menus can be added to a sidebar as a widget, but areas in your template can be set to use a custom menu.
The default Twenty Ten template comes with one custom menu area in the template. If you want to change your WordPress templates to support custom menus in areas other than a sidebar there area couple of steps to take. First thing that needs to be done is to register the menu(s) in the functions.php file using the register_nav_menus() function.
register_nav_menus( array( 'primary' => __( 'Primary Navigation' ), ) );
In this function we use an array to list out the different menus available. The first is the name used in the template code, the second part between the parenthesis is the Label that will show in the interface. Only one is shown above, but more that one can be defined.
register_nav_menus( array( 'primary' => __( 'Primary Navigation' ), 'secondary' => __( 'Secondary Navigation' ), 'tertiary' => __( 'Tertiary Navigation' ), ) );
For backwards compatibility you should also check to see if the function exists before calling it. More information on this function can be found in the WordPress codex.
if ( function_exists( 'register_nav_menus' ) ) {
register_nav_menus( array(
'primary' => __( 'Primary Navigation' ),
'secondary' => __( 'Secondary Navigation' ),
'tertiary' => __( 'Tertiary Navigation' ),
) );
}
Once you have defined your navigation options in the functions.php file, all that is left is to perform a call to the navigation in your templates. You can call your custom navigation using the wp_nav_menu() function. The great thing about this function is that it is backwards compatible and if no custom nav is set in the WordPress backend, it will default to listing your pages.
<?php wp_nav_menu( array( 'theme_location' => 'primary' ) ); ?>
There are more options you can use with this function, this is just the minimum. Notice the theme location 'primary 'is the same as was defined in functions.php.
More information on this function can be found in the WordPress codex.
Add Your Comment
You must be logged in to post a comment.