Advertisement

Add the Custom Menus Feature to a WordPress Theme

The WordPress Custom Menus feature is already built in to the default Twenty Ten theme, so you don't have to worry about preparing your theme for it. However, if you're using a different theme, or creating your own, follow these steps to add this functionality:

  1. In the Dashboard, choose Appearance→Editor.

    The Edit Themes page appears.

  2. Click the Theme Functions (functions.php) template.

    The Theme Functions template opens in the text editor in the center of the Edit Themes page.

  3. Type the following function on a new line in the Theme Functions template file:

    // ADD MENU SUPPORT
    add_theme_support( 'nav-menus' ); 

    This template tag tells WordPress that your theme can use the Custom Menu feature.

  4. Click the Update File button to save the changes to the template.

    A Menus link displays in the Appearance menu.

    Next, you want to add the menus template tag to the Header template (header.php).

  5. On the Edit Themes page, open the Header template (header.php).

    The Header template opens in the text editor in the middle of the Edit Themes page.

  6. Add the following template tag by typing it on a new line in the Header template (header.php):

    <?php wp_nav_menu(); ?>

    This template tag is needed so the menu you build using the Custom Menu feature displays at the top of your website. The table gives the details on the different parameters you can use with the wp_nav_menu(); template tag to customize the display to suit your needs.

  7. Click the Update File button to save the changes you've made to the Header template.

    The navigation menu that you build on the Menus page in your Dashboard (choose Appearance→Menus) will now appear in the header area of your website.

Common Tag Parameters for wp_nav_menu();
Parameter Information Default Tag Example
id Unique ID of the menu (because you can create several menus, each has a unique ID number) Blank wp_nav_menu( array( 'id' => '1' ) );
slug Menu name in slug form (for example, nav-menu) Blank wp_nav_menu( array( 'slug' => 'nav-menu' ) );
menu Menu name Blank wp_nav_menu( array( 'menu' => 'Nav Menu' ) ); or wp_nav_menu('Nav Menu');
menu_class CSS class used to style the menu list Menu wp_nav_menu( array( 'menu_class' => 'mymenu' ) );
format HTML markup used to style the list, either an unordered list (ul/li) or div class div wp_nav_menu( array( 'format' => 'ul' ) );
fallback_cb Parameter that creates a fallback if a custom menu doesn't exist wp_page_menu (a default list of page links) wp_nav_menu( array( ' fallback_cb' => 'wp_page_menu') );
before Text that displays before the link text None wp_nav_menu( array( 'before' => 'Click Here' ) );
after Text that displays after the link text None wp_nav_menu( array( 'after' => '&raquo;' ) );
blog comments powered by Disqus
Advertisement
Advertisement

Inside Dummies.com