In my theme, I am adding a menu upon theme activation using
add_action( 'after_setup_theme', 'mycustom_register_menu' );
function mycustom_register_menu() {
register_nav_menu( 'somelocation', __( 'My Menu') );
}
I added the location in my theme's header.php, now all that's left is to create a menu in the dashbaord and assign it to that location. It works perfectly when I do so manually.
My question is, how can I automate it? so when the theme is activated and "My Menu" is added as a location, a menu with a certain name is created and assigned to that location? Is there a hook for that?
Creating Custom Navigation Menus in WordPress Theme
function wpb_custom_new_menu() {
register_nav_menu('my-custom-menu',__( 'My Custom Menu' ));
}
add_action( 'init', 'wpb_custom_new_menu' );
If you want to add more than one new navigation menu location, then you would need to use a code like this:
function wpb_custom_new_menu() {`enter code here`
register_nav_menus(
array(
'my-custom-menu' => __( 'My Custom Menu' ),
'extra-menu' => __( 'Extra Menu' )
)
);
}
add_action( 'init', 'wpb_custom_new_menu' );
Displaying Custom Navigation Menus in WordPress Themes
<?php
wp_nav_menu( array(
'theme_location' => 'my-custom-menu',
'container_class' => 'custom-menu-class' ) );
?>