Register navigation menu in wordpress custom theme

Register navigation menu in wordpress custom theme

Friends, navigation is very important for any site, as you know, almost all the pages in any site are linked to the navigation menu and there are some pages which are linked through a link.

To create a navigation menu in WordPress, there is an option in the WordPress admin panel which is found in the menu section of Appearance.

WordPress includes an easy to use mechanism for introducing customised navigation menus into a theme. In order to incorporate menu support into your theme, you need to add a few code segments to your theme files.

Register nav menu:

register_nav_menus()
register_nav_menu()
unregister_nav_menu()
Show nav menu:

has_nav_menu()
wp_nav_menu()
register_nav_menus (in functions.php page):

<?php 

if ( ! function_exists( 'mytheme_register_nav_menu' ) ) {

function mytheme_register_nav_menu(){
  register_nav_menus( array(
    'primary_menu' => __( 'Primary Menu', 'Main Menu' ),
    'footer_menu'  => __( 'Footer Menu', 'Footer Menu' ),
  ) );
}
add_action( 'after_setup_theme', 'mytheme_register_nav_menu', 0 );

}

?>

OR

register_nav_menus( array(
    'primary_menu ' => __( 'Primary Menu', 'Main Menu' ),
    'footer_menu'  => __( 'Footer Menu', 'Footer Menu' ),
) );
header.php (wp_nav_menu()):

<?php 
   wp_nav_menu( array( 'theme_location' => 'primary_menu' ) ); 
?>


<?php 

wp_nav_menu( [ 
 'theme_location' => 'primary_menu',
 'menu_class' => 'nav pknav'
] );

?>

Create pages or category or custom links for showing this menu on frontend side.

 

Show all kinds of registed menu:

print_r (get_registered_nav_menus());
// Array ( [primary_menu] => Primary Menu 
        [footer_menu] => Footer Menu )

Leave a Reply