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.

hey all,
i'm writing an automated function that generates a menu from a function. it calls wp_nav_menu on each item from an array, because i want this to be dynamic. the problem is, no matter how i set it, if the menu doesn't exist, wp_nav_menu is generating a menu, eg the 'default'. here is my code (items is just a set of strings):

for($i=0;$i<count($items);$i++) {

    $themenu=$items[$i];

    $mymenu = wp_nav_menu(array(
            'menu' => $themenu,
            'menu_class' => 'mymenu',
            'container' => 'false',
            'fallback_cb' => 'false',
            'echo' => false
            )
        );  

    echo $themenu;

    }

i know its partially working, because if $themenu exists, it shows the correct one. but if it doesn't, it'll just show any menu! not just annoying, but also actively breaks the user experience.

share|improve this question
    
Try to set 'fallback_cb' => '' –  Bainternet Mar 31 '11 at 0:09
    
i tried that, no luck. –  dama_do_bling Mar 31 '11 at 0:27
    
can you show more of your code so we could see whats causing this? also you can try to create a stub function that does nothing and use that as you fallback. –  Bainternet Mar 31 '11 at 0:33
    
sure, edited above to reflect the whole loop... nothing super complicated –  dama_do_bling Mar 31 '11 at 0:56
    
size($items)? Do you mean count($items)? Or did you define a size() function elsewhere? –  Gavin Anderegg Mar 31 '11 at 1:12

3 Answers 3

wp_nav_menu() indeed tries a lot to provide you with a menu, and fallback_cb is only executed when nothing else works. From the code:

  • If menu is provided and refers to an existing menu (looked up via wp_get_nav_menu_object(), which accepts an id, slug or name), this will be the menu
  • Otherwise, if theme_location is set to a registered menu location, this will be passed to wp_get_nav_menu_object()
  • Otherwise, WordPress will search for the first existing menu that has items and use that
  • Otherwise, fallback_cb is called, which by default is wp_page_menu, which is a menu of all the pages

So if you only want to use the menu argument, you should test this yourself by calling wp_get_nav_menu_object(). Only if this returns something you should call wp_nav_menu().

share|improve this answer
    
jan, thats the test i was looking for! undocumented, that is. thanks. –  dama_do_bling Apr 6 '11 at 12:32
    
@yonation: Well, the code is documentation too :-) –  Jan Fabry Apr 6 '11 at 12:35
    
I'm having the same problem but I don't follow this answer. Do you mind sharing your code? –  Ryan Nov 2 '11 at 19:22

Try wrapping your echo inside of a has_nav_menu() conditional:

for($i=0;$i<size($items);$i++) {

    $themenu=$items[$i];

    $mymenu = wp_nav_menu(array(
            'menu' => $themenu,
            'menu_class' => 'mymenu',
            'container' => 'false',
            'fallback_cb' => 'false',
            'echo' => false
            )
        );  

    if ( has_nav_menu( $themenu ) ) echo $themenu;

    }

(If I'm following your code correctly...)

share|improve this answer
    
hi chip thanks for the suggestion, but has_nav_menu only refers to menu location not slug/name. i am not working with locations for this particular theme, so we are back at the beginning. it amazes me there is no has_nav_menu_name or something... –  dama_do_bling Mar 31 '11 at 2:58

From the Codex entry for wp_nav_menu():

$fallback_cb (string) (optional) If the menu doesn't exist, the fallback function to use. Set to false for no fallback. Default: wp_page_menu

So have you tried passing 'fallback_cb' => false?

EDIT:

As per the comment below, 'fallback_cb' => 'false' is telling wp_nav_menu() to fallback to a function called false(), and since this function doesn't exist, it falls back to its normal fallback, wp_page_menu(). So, use 'fallback_cb' => false (i.e. a boolean value, rather than a string value).

share|improve this answer
    
Er, I guess you did. Hmm... maybe it needs to be false, rather than 'false'? Grasping at straws here. –  Chip Bennett Mar 31 '11 at 3:18
    
@Chip Bennett Yes, 'false' refers to an impossible function false() while just false is a boolean value. –  toscho Mar 31 '11 at 8:08
    
chip and toscho, thanks for the help. i see the difference but both false and 'false' still give me the default menu. i think the problem lies in the core include (i've been tooling around there) - fallback is used if the LOCATION isn't found, not if the menu. if the menu isn't found, then it does whatever the hell it wants, which is a really weird reaction. basically i need to - but don't want, for obvious upgrade & other reasons - edit wp-includes/nav-menu-template.php. –  dama_do_bling Mar 31 '11 at 13:26
    
Is there a reason that you can't register/use a theme_location for each of these dynamic menus? –  Chip Bennett Mar 31 '11 at 13:31
    
no reason for me personally, though a)i see it as an extra unnecessary step and b)i'm a web dev and trying to think of this as the enduser would; if they want to create a new submenu that would automatically attach to existing elements, i want it as dynamic as possible. theme_location makes that flow impossible. frankly i do not for the life of me get why they wouldn't let you fallback on NO menu rather than a default, seems like a big error. –  dama_do_bling Mar 31 '11 at 13:38

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.