How to Make Your WordPress Theme Compatible With Menus
- 1). Log into your WordPress dashboard and navigate to "Appearance." Click "Editor" under "Appearance" and click on the link under "Theme Functions" on the right side of the theme editing box. If you work on theme files on your computer, you can open "functions.php" in your code editor instead. That file is found here:
/yoursite/wp-content/themes/theme-folder/ - 2). Add this code somewhere in your "functions.php" file:
if (function_exists('register_nav_menus')) {
register_nav_menus(
array(
'first_menu_name' => 'My First Menu',
'second_menu_name' => 'My Second Menu'
)
);
}
This function checks whether the register_nav_menus() function exists in your current version of WordPress and then uses that function to register an array of menus. You can add one, two or any other number of menus you want using this function. - 3). Edit the template where you wish to add a menu. Most themes put at least one menu in the "header.php" file, and sometimes in the "footer.php" file as well. Here is the code:
<?php wp_nav_menu(); ?>
The above code is generic and does not include any parameters. Use parameters to configure your menus programmatically. Here is an example using wp_nav_menu() to call a menu with a specific name, which the user would then need to create in the "Menus" panel of the dashboard:
<?php wp_nav_menu(array('menu' => 'Top Menu')); ?> - 4). Navigate to the "Menus" panel under "Appearance" in the WordPress dashboard. You should see the menus you created. Check boxes next to links you want in the lists on the left side and then click "Add" to place them in a menu. Rearrange the links by dragging and dropping them. Indented links in the menu indicate second- and third-level links. Click the tabs on the top to switch between menus.