Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a register_nav_menus call in my theme's functions.php:

//register nav menu and footer nav.
register_nav_menus(
    array(
        'main-nav'   => 'Main Navigation',
        'footer-nav' => 'Footer Navigation'
    )
);

This works fine, but I'd like to take the extra step of programatically creating a menu and assigning it to 'main-nav', set to automatically add new pages.

This theme is being used as a starting point for a wordpress install, so I'd like to include this functionality to save a little bit of time on the repetitive tasks such as manually creating these menus via appearance -> menus.

Does anyone know how to do this?

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

Figured out how to do this based on an answer to this question : Programmatically add a Navigation menu and menu items

I adapted my answer to suit my own needs, i.e just to create the nav menu and assign it to the location I'd already defined:

//register nav menu and footer nav.
register_nav_menus(
    array(
        'main-nav'   => 'Main Navigation',
        'footer-nav' => 'Footer Navigation'
    )
);

//now see if the main navigation menu is there - if not, create it.
if (!wp_get_nav_menu_object('Main Navigation'))
{
    $menu_id = wp_create_nav_menu('Main Navigation'); //create the menu
    $locations = get_theme_mod('nav_menu_locations'); //get the menu locations
    $locations['main-nav'] = $menu_id; //set our new menu to be the main nav
    set_theme_mod('nav_menu_locations', $locations); //update 
}

Downsides to this is that if the admin deletes the main navigation menu, it will get created again (which could get annoying) - so I've included the create menu code in a function which only runs once, on theme install. There are no pages added with this code, but having suitable menues defined and already assigned to their theme-locations will simplify matters for non-coders creating sites with my theme, and no more questions about why certain pages are showing in the nav menu when a nav menu hasn't been created yet :)

share|improve this answer
add comment

There is no need to create/update custom menus programmatically, just to do what you're trying to do.

Just rely on the wp_nav_menu() fallback, wp_page_menu(), or define your own, custom callback, e.g. to output wp_list_pages().

Calling this:

`wp_nav_menu( array( 'theme_location' => 'main-nav' ) );`

...will, if no custom menu has been assigned to the 'main-nav' theme location, output this:

`wp_page_menu()`.

If you want more fine-grained control, e.g. to control the menu depth explicitly, you could do the following:

wp_nav_menu( array(
    'theme_location' => 'main-nav',
    'depth' => 3,
    'container_class' => 'nav',
    'menu_id' => 'main-nav',
    'fallback_cb' => 'wpse87933_main_nav_cb'
) );

...and then define your callback like so:

function wpse87933_main_nav_cb() {
    ?>
    <div class="nav">
        <ul id="main-nav">
            <?php
            wp_list_pages( array(
                'depth' => 3,
                'title_li' => ''
            ) );
            ?>
        </ul>
    </div>
    <?php
}

You could even simplify things by using the has_nav_menu() conditional:

if ( has_nav_menu( 'main-nav' ) ) {
    wp_nav_menu( array(
        'theme_location' => 'main-nav',
        'depth' => 3,
        'container_class' => 'nav',
        'menu_id' => 'main-nav'
    ) );
} else {
    ?>
    <div class="nav">
        <ul id="main-nav">
            <?php
            wp_list_pages( array(
                'depth' => 3,
                'title_li' => ''
            ) );
            ?>
        </ul>
    </div>
    <?php  
}

At that point, you just need to be mindful of the CSS class differences between the 3 functions:

share|improve this answer
    
great answer, and very useful info - but I still do want to create the menus themselves programatically. I know I don't have to but whenever I install my theme the first thing I do is create the main nav menu as 99% of the time it is always a custom menu. Your point about wp_nav_menu falling back is good to know though :) –  jammypeach Feb 22 '13 at 13:46
    
Why do you want to reinvent the wheel? Why bother with programmatically creating custom navigation menus to be assigned to Theme Locations, when WordPress already provides much easier ways to ensure that those Theme Locations can display dynamic menus, with no intervention necessary? –  Chip Bennett Feb 22 '13 at 13:57
    
Because when I make a WP site, most of the time I want a specific set of top-level pages in the nav menu (not all of them). This means install wp -> install theme -> add some example pages -> add menus, which is inefficient when you do it 10 times. It would be a time-saver to auto-generate the menu on theme install, then make appropriate edits later. The other side of the coin is that the theme is used by some of my collegues and I often have to go in and create the main nav menu for them; they don't realise it needs to be created each time. –  jammypeach Feb 22 '13 at 14:06
    
if you know of an easier / simpler way to acheive that I'm definitely all ears :) –  jammypeach Feb 22 '13 at 14:07
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.