2

I just realized the fallback doesn't work for my project when a custom menu doesn't exist. I can't figure out what I am missing. Here is the code that displays the nav menu:

$menu = wp_nav_menu(
   array(
      'menu_class' => 'reset sf-menu',
      'container_id'=>'nav',
      'echo'=>0
   )
);

$menu = str_replace("\n", "", $menu);
$menu = str_replace("\r", "", $menu);
$menu = str_replace("\t", "", $menu);
echo $menu;

here is the test site: http://brainbuzzmedia.com/themes/amplify/styles/charcoal/

  • 1
    What exactly does it return? Check with var_dump() on $menu. – Rarst Aug 7 '11 at 14:57
  • wp_nav_menu falls back to the wp_page_menu function. Do you have any pages defined? – Otto Aug 7 '11 at 16:07
  • I have the default "About" page defined as the only page, I also tried adding two more pages and that didn't work either. – Brainbuzz Aug 7 '11 at 19:58
3

You've not added a fallback_cb key to the argument array. You also need to add a theme_location key:

php start $menu = wp_nav_menu(
   array(
      'menu_class' => 'reset sf-menu',
      'container_id'=>'nav',
      'echo'=>0,
      'fallback_cb' => 'mytheme_fallback_function' // ADD ME!!!
      'theme_location' => `location_name` // ADD ME, AS DEFINED IN register_nav_menu()
   )
);

$menu = str_replace("\n", "", $menu);
$menu = str_replace("\r", "", $menu);
$menu = str_replace("\t", "", $menu);
echo $menu;
php end

Alternately, you could wrap your code in an if ( has_nav_menu( 'location_name' ) ) conditional:

if ( has_nav_menu( 'location_name' ) ) {
    echo $menu;
} else {
    // Do something else, like wp_list_pages() or wp_page_menu()
}
  • 2
    Actually, it's "fallback_cb", not "fallback". Also, it defaults to the wp_page_menu function, so you shouldn't need to define it except for non-default cases. – Otto Aug 7 '11 at 16:06
  • 1
    Is WPSE experiencing a bug or did something mess up your end, because i'm seeing php start and php end in your above code, which should naturally be <?php and ?>.. – t31os Aug 7 '11 at 16:14
  • yeah that is just a bug in the wpse displaying things so i replaced the php tags here – Brainbuzz Aug 7 '11 at 19:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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