If you are interested in developing a custom theme created by you, you should know from the beginning that certain features do not appear by default. These are required through specific WordPress features. A newly created theme will need to implement some PHP functions (usually added in the functions.php file) that require the CMS to activate certain functionalities or parts such as: sidebar, menu (for header, and footer) etc.
This is not necessarily difficult. In this article we will show you how to enter a menu, using the following code:
if ( ! function_exists( 'meniu_custom' ) ) {
function meniu_custom() {
$locations = array(
'menu name' => __( 'Meniu', 'text_domain' ),
);
register_nav_menus( $locations );
}
add_action( 'init', 'meniu_custom' );
}
Brief explanation of the code:
- A conditional (if) statement is created specifying that the function named custom_menu () be created if it does not exist. The name of the custom_menu () function can be replaced with anything else you want.
- The menu_custom () function registers via another default WordPress function (register_nav_menus ()) a new menu named in the example given “menu name”.
- In order to activate the new menu, we will use a WordPress Action Hook, respectively add_action () which will contain at least two arguments: the first will say what type of action is invoked (in our case “init”) and the second will specify the function in which the menu was created.
This code can be used in the theme’s functions.php file and customized according to the needs of your site.
Now we have created a menu that can be found and configured in the WordPress control panel. Next we will have to display it in the header.php page of the theme via the wp_nav_menu () function. How do we do that? using the following code:
wp_nav_menu( array(
'theme_location' => 'menu name',
'menu_id' => 'primary-menu',
'depth' => 0,
'container_class' => 'nav navbar',
) );