How do I add support to my theme for custom menus?

I’ve read a few tutorials on how to add the functionality of the custom menus added in version 3.0 to my theme but they all seem to differ somewhat, and I’m not sure whether they contain superfluous code. In addition I read that the code to register your theme as menu-compatible changed between the RC and the public release.

What’s the simplest way of adding support for custom menus to my theme?

Related posts

Leave a Reply

3 comments

  1. The easiest way is to use the register_nav_menus function.This should be hooked into 'after_setup_theme':

    function my_cool_menu_function(){
      register_nav_menus( array(
        'primary' => 'Primary Navigation'
      ));
    }
    
    add_action( 'after_setup_theme', 'my_cool_menu_function' );
    

    Then, in your theme, simply call that menu’s position:

    wp_nav_menu( array( 'theme_location' => 'primary' ) );
    
  2. Add this to functions.php of your theme. If you don’t have one, create it.:

    <?php
    add_theme_support('menus');
    function register_my_menus() {
        $args = array( 
            'menu-1' => __( 'Main Menu' )
        );
        register_nav_menus( $args );
    }
    add_action( 'init', 'register_my_menus' );
    ?>
    

    And this can go anywhere, I have it in header.php (see wp_nav_menu() docs):

    <?php
    $args = array(
        'menu' => 'Main Menu',
        'sort_column' => 'menu_order',
        'container' => 'div',
    );
    wp_nav_menu( $args ); ?>
    
  3. This how to include the custom navigation in its simplest form, I believe it picks up the navigation with not identifier:

    <div class="access">
      <?php wp_nav_menu(); ?>
    </div>
    

    There are also basic options that can be passed to the function, here’s what I generally use:

    $navArgs=array(
        'menu_id' => 'nav',    // target the nav menu
        'container' => '',     // don't wrap the menu in <ul>
    );