How to Make Your Theme Custom Menu Compatible
When developing your own themes, developing your own theme locations could be important to the creation of your theme. The basics of integrating menu support involve defining menus (the name of our theme locations) and then calling them in our templates.
First, let’s define some menus. Add the following code to your theme’s functions.php file:
function register_my_menus() {
register_nav_menus(
array(
'top-menu' => __( 'Top Menu' ),
'footer-menu' => __( 'Footer Menu' )
)
);
}
add_action( 'init', 'register_my_menus' );
This function assumes that we’re going to be building two menus, one for the main navigation and one for the footer. Now if we were to call the menu in our header.php or footer.php files, they would look like this:
<?php wp_nav_menu( array( 'theme_location' => 'top-menu' )); ?>
or
<?php wp_nav_menu( array( 'theme_location' => 'footer-menu' )); ?>
Creating your own menu locations for themes can be used in a variety of ways, especially if WordPress conditional tags are employed. For example, let’s say we wanted a user to see a menu only if they’re logged in. We could place the following code in the header.php file:
<?php
if ( is_user_logged_in() ) {
wp_nav_menu(array('theme_location'=>'user-menu'));
} else {
wp_nav_menu(array('theme_location'=>'main-menu'));
}
?>
Conditional tags offer us the ability to show different menus on all kinds of system pages like archives, categories, specific pages, etc. You can find a list of conditional tags on the WordPress Codex.
Reference:
http://www.developerdrive.com/2012/02/how-to-create-and-use-wordpress-custom-menus/
http://codex.wordpress.org/Navigation_Menus
0 comments:
Post a Comment