Add Menu Support to your WordPress Theme

WordPress 3.0 includes a menu navigation system that is handy, especially when your handing a themed site off to the customer. Below is a way to add menu support to your WordPress theme while maintaing compatibility in case a menu is not created.

In your theme’s functions.php add the following code:

<?php
function mytheme_addmenus() {
	register_nav_menus(
		array(
			'main_nav' => 'The Main Menu',
		)
	);
}
add_action( 'init', 'mytheme_addmenus' );
function mytheme_nav() {
    if ( function_exists( 'wp_nav_menu' ) )
        wp_nav_menu( 'menu=main_nav&container_class=pagemenu&fallback_cb=mytheme_nav_fallback' );
    else
        mytheme_nav_fallback();
}
function mytheme_nav_fallback() {
    wp_page_menu( 'show_home=Start&menu_class=pagemenu' );
}
?>

The first function adds support for the menus. You will use the mytheme_nav() function in the theme to display the menu. In the function, we check if wp_nav_menu() exists and fallback to mytheme_nav_fallback() if we are on pre 3.0 WordPress (or if a menu has not been created).

In your template use the following code to use your new custom function:

<?php mytheme_nav(); ?>

Share Your Thoughts

Your email address will not be published. Required fields are marked *