How to Display Custom Menus in WordPress Using Widgets

By Lisa Sabin-Wilson

You don’t have to use the wp_nav_menu(); template tag to display the menus on your site because WordPress also provides you with a Custom Menu widget that you can add to your theme, allowing you to use widgets, instead of template tags, to display the navigation menus on your site. This is especially helpful if you’ve created multiple menus for use in and around your site in various places.

Your first step is to register a special widget area for your theme to handle the Custom Menu widget display. To do this, open your theme’s function.php file and add the following lines of code anywhere before the closing ?>:

// ADD MENU WIDGET
if ( function_exists('register_sidebars') )
    register_sidebar(array('name'=>'Menu',));

These few lines of code create a new Menu Widget Area on the Widgets page on your Dashboard. At this point, you can drag the Custom Menu widget into the Menu Widget Area to indicate that you wish to display a custom menu in that area. If you have more than one menu, you can select which menu to display from the Select Menu drop-down list.

image0.jpg

To add the Menu Widget Area to your theme, head over to the Edit Themes page (choose Appearance→Editor), click the Header template (header.php) file, and add these lines of code in the area you want the Menu Widget Area displayed:

<ul>
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Menu') ) : ?>
<?php endif; ?>
</ul>

These lines of code tell WordPress that you want information contained in the Menu Widget Area displayed on your site.