WordPress: Add Custom Menus
To add custom menus to your WordPress theme just add the following to your themes functions.php file, be sure to place this code inside your PHP code <?php and ?>. When we declare a menu we first have the identification and secondly we have the title. For instance in the following we will create three custom menus (You can create as many or as few as you like).
- The First will have an ID of ‘primary’ and be named ‘Main Nav’ (Note: The name can have spaces, the ID can not).
- The Second will have an ID of ‘secondary’ and be named ‘Sub Nav’.
- The Third will have an id of ‘anothermenu’ and be named ‘Side Nav’.
1 2 3 4 5 6 7 8 9 10 11 12 |
/*Custom Menu Support*/ add_action( 'init', 'register_my_menus' ); function register_my_menus() { register_nav_menus( array( 'primary' => __( 'Main Nav' ), 'secondary' => __( 'Sub Nav' ), 'anothermenu' => __( 'Side Nav' ) ) ); } |
You can call these menus in your theme with the following code:
1 |
<?php wp_nav_menu(array('theme_location' => 'primary')); ?> |
1 |
<?php wp_nav_menu(array('theme_location' => 'secondary')); ?> |
or
1 |
<?php wp_nav_menu(array('theme_location' => 'anothermenu')); ?> |
Recent Comments