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.

When I use

wp_nav_menu(array(
    'theme_location' => 'mainnav',
    'container' => 'nav',
    'container_id' => 'mainnav',
    'container_class' => 'clearfix',
    'depth' => 4
)); 

If the user hasn't set a nav menu under Appearance menu, I notice theres a fallback like

<div class="menu"><ul><li class="page_item page-item-2"><a title="Sample Page" href="http://dev.klifmedia.net/ae/?page_id=2">Sample Page</a></li></ul></div>

How can I have the fallback use whats specified in wp_nav_menu? How can I check if theres a menu to display then display it, else show nothing?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

The default fallback menu is created with wp_page_menu() function and if you want to customize it the you need to create your own fallback function :

function my_fallback_menu(){
   $args =array('whatever');
   wp_page_menu($args);
}

and then call that function in your wp_nav_menu call as the fallback:

wp_nav_menu(array(
    'theme_location' => 'mainnav',
    'container' => 'nav',
    'container_id' => 'mainnav',
    'container_class' => 'clearfix',
    'depth' => 4,
    'fallback_cb' => 'my_fallback_menu'
)); 

to check if a menu exists you can use has_nav_menu() function:

if (has_nav_menu( 'mainnav' )){
    //display the menu
}else{
    //no menu yet do something else
}

and to simplify the things even more you can just call the menu and set the fallback to false:

wp_nav_menu(array(
    'theme_location' => 'mainnav',
    'container' => 'nav',
    'container_id' => 'mainnav',
    'container_class' => 'clearfix',
    'depth' => 4,
    'fallback_cb' => false
)); 
share|improve this answer

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.