• when I activate my theme, it looks bad without the wp_nav_menu set up. So I am trying to code for that exception. So I tried wp_list_pages

    
    
    if(wp_nav_menu('primay')){
      wp_nav_menu();
    }
    else {
       wp_list_pages
    }

    My problem is that wp_nav_menu is always used even though I am testing for the nav menu called primary which at this point should not be set up and working.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Hi, I haven’t tested this but wouldn’t it be something like:

    if(wp_nav_menu(array('menu'=>'primary')){
    //Do Stuff
    } else {
    wp_list_pages();
    }

    If it’s not that there are some other nav functions in the codex, like get_wp_nav_menu I think? Either way I expect there is a simple enough way to test for the menu.

    What’s it doing at the moment? Just generating an empty menu?

    Dan

    Thread Starter cybershot

    (@cybershot)

    I have tried this code and it just doesn’t work. I don’t understand why.

    I have styling on the menu and the menu displays perfect. But If I throw in the if statement then it breaks the styling. I tried this

    
    
    <?php if(wp_nav_menu(array('menu' => 'primary'))){
    					 wp_nav_menu( array( 'theme_location' => 'primary','container' => 'div','container_id' => 'navigation','link_before' => '<span>','link_after' => '</span>' ) );
    			} else {
    				echo "testing";
    			} ?>

    but this showed both the menu and the testing

    the reason I am doing this is because when I activate the theme, the default menu shows and it has no styling. So I am trying to get control over what menu shows. This just isn’t working

    When you register a navigation menu in the first place (in your functions.php file), you have the option to define a fallback function that will run if the nav menu doesn’t exist. There should be no need to test for the nav menu, as that test is already performed by the WordPress core, which then calls the fallback function if it doesn’t exist.

    I’m not actually familiar with the registering of a menu. When I make a site now I go into appearance->themes and make a new menu and add my pages. Say I call it “main nav”. Then I go into my theme and drop in;
    <?php wp_nav_menu(array('menu'=>'main nav')); ?>
    That generates a ul with a load of other styling which can be reduced as you put in your example. I don’t do anything else….

    Thread Starter cybershot

    (@cybershot)

    I understand all that. The problem is this.

    I have styled my menu a specific way. So when that menu isn’t present. The nav area looks bad. I thought that if wordpress isn’t using wp_nav_menu then it uses wp_list_pages. I can’t get it to work. I want the nav area to look good no matter what menu is working. I have checked the codex and I don’t see how to set the callback function when registering menus

    Have you tried a simple if/else statement on the wp_nav_menu? Like if there is a nav echo yes, else no? If that works there really should be a problem. If there is a nav is it dropped into a div styled a certain way? And if so, is it the same styling for wp_list_pages? It might be helpful to see what your working on, at the moment I can’t visualise the problem…

    The fallback for the wp_nav_menu is this in the codex.

    That’s it…thank you g3legacy.

    This is what I did:

    <?php
    if(wp_nav_menu( array( 'theme_location' => 'primary', 'fallback_cb' => 'false') )) {
    echo wp_nav_menu( array( 'sort_column' => 'menu_order', 'container_class' => 'menu-header', 'theme_location' => 'primary' , 'echo' => '0' ) );
    }
    else
    {
    // there's no custom menu created.
    }
    ?>

    Great ?? I’m pleased I helped someone!

    @dew_h2o
    I tried your code and it worked OK, insofar as the menu didn’t show up if it was empty–however, the container_class was ignored in the output.

    What worked for me is this:

    <?php if ( has_nav_menu( 'primary' ) ) : ?>
    <?php wp_nav_menu( array( 'container_class' => 'menu-primary', 'theme_location' => 'primary' ) ); ?>
    <?php endif; ?>

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘how to test for wp_nav_menu’ is closed to new replies.