Adding new menu to WordPress theme to be customised by the admin

So a friend messages me about custom navigation that can be edited within the Content Management System(CMS) of wordpress and not hard coded into the website template. Of course i’ve only done a little in wordpress and php but this seemed like an interesting thing to know so i set about looking it up, and here is what i’ve found.

Bare in mind this is tested with the TwentyTen Theme, the first thing to do is find the call to register_nav_menus() function in the functions.php file of the theme, maybe in the setup function. and add an extra entry to the array using ‘my_custom_nav’ => ‘Custom Side Nav’,

register_nav_menus( array(
'primary' => __( 'Primary Navigation', 'twentyten' ),
'my_custom_nav' => 'Custom Side Nav',
) );

The my_custom_nav is the key and should also be the name of the function for it (you can choose the name you want but you’ll need to keep it consistent throughout), and Custom Side Nav is what it will be called in the admin page under Appearance -> Menus (again call it what you want). Then outside of the setup function add a new function with the name my_custom_nav. Make sure that it is still within some php tags though.


function my_custom_nav(){
wp_nav_menu(array('container_class' => 'navigation', 'theme_location' => 'my_custom_nav'));
}

Here the container_class and theme_location are literal names so do not change, well they’re keys actually i guess. navigation didn’t seem to make any difference when changing its name, but my_custom_nav is a name i chose but must be the same name as the function itself and the same name as the last parameter of the php in the next step when adding to the layout templates. But that should be all that’s needed to add the extra menu when logged in as admin, Appearance -> Menus.

To display this menu in the theme you would need the following php in your template where you want the nav to be.

<?php wp_nav_menu( array( 'container_class' => 'menu_class_name', 'theme_location' => 'my_custom_nav' ) ); ?>

Here menu_class_name is the html class name you want added to it(you can choose), and my_custom_nav is the key and function name from the previous steps.

This should be all to get it working, though i cannot guarantee it will work for all themes, but this is mostly as a note to self anyway so i don’t forget. Sometime you may need to refresh the menus and resave them from the admin panel if you’ve been changing names in the functions.php file.

If text isn’t colour coded in your text editor, then it maybe that you’ve put the code outside of the php tags, if so then just wrap the tags around it so <?php then the code and then finsih with a ?>. Other problems maybe the single quotes are using the wrong ascii key so replace them or something. Thanks for reading.

This entry was posted in General, lartens, Tech and tagged , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *