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'm working on a fallback menu for a WordPress theme I'm working on. The main menu's menu_class renders a ul which is what I want.

However, if I use the menu fallback, the menu_class renders a div instead. Is there a way to make Wordpress render a ul for the fallback menu instead of a div?

Here's my code:

 function clarity_main_nav_fallback() {

     wp_nav_menu(array(

    'show_home' => true,
    'container' => false,                           // remove nav container
    'container_class' => 'menu clearfix',           // class of container (should you choose to use it)
    'menu_class' => 'nav navbar-nav'              // adding custom nav class

    ));

 }
share|improve this question
add comment

1 Answer 1

up vote 3 down vote accepted

First, I think you're confusing the wp_nav_menu() args. The 'menu_class' parameter defines the class added to the menu element, which by default is <ul>, as per the 'items_wrap' parameter. The default 'menu_class' is 'menu', which results in <ul class="menu">.

The real issue is actually the fallback_cb - the callback used when no menu is defined - which defaults to wp_page_menu().

Now, both wp_nav_menu() and wp_page_menu() output an unordered list (<ul>) wrapped by a container (<div>) by default. In your call to wp_nav_menu(), you have: 'container' => false, which overrides the menu list being wrapped by a container <div>. But that parameter does not get passed to the default wp_page_menu() callback output.

The easiest solution would be to define your own callback:

wp_nav_menu( array(
    // Add your normal args here
    'fallback_cb' => 'wpse116656_nav_menu_cb'
) );

Then declare your callback function:

function wpse116656_nav_menu_cb() {
    wp_page_menu( array(
        // Args here
    ) );
}

Now, here's the caveat:

  • wp_nav_menu() applies the 'menu_class' parameter to the unordered list

    (<div><ul class="$menu_class"></ul></div>)

  • wp_page_menu() applies the 'menu_class' parameter to the wrapper container outside the menu list

    (<div class="$menu_class"><ul></ul></div>)

So, you'll need to account for that with your CSS.

share|improve this answer
    
You are right @Chip Bennet! I was totally confused. –  Johann Oct 3 '13 at 16:44
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.