Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to hide the div class "navmain2" if the menu is empty. See code below:

          <!-- Start main navigation -->

    <div class="navmain2">
<div id="logo"></div>

        <!-- Gets main menu by id -->
        <span></span>
            <?php wp_nav_menu( array(
            'menu' => 11,
            'container' =>false,
            'menu_class' => 'nav',
            'echo' => true,
            'before' => '',
             'after' => '',
             'link_before' => '',
             'link_after' => '',
             'depth' => 0,
            'walker' => new description_walker())
             );?>

        <!-- /main menu -->

  <div id="klicka">Click here! Click here! Click here</div>

    </div><!-- /main navigation -->-->

I have a logo and a div with text inside navmain2 as you can see.

The question is:

Is it possible to hide the whole navmain2 div if the wp_nav_menu is empty?

share|improve this question

2 Answers

up vote 2 down vote accepted

Assign the menu to a string:

$menu = wp_nav_menu(
    array (
        'echo' => FALSE,
        'fallback_cb' => '__return_false'
    )
);

if ( ! empty ( $menu ) )
{
    echo '<div class="navmain2">' . $menu . '</div>';
}
share|improve this answer
So should i edit the code or this is a seperate function that ill add in the same page ? – Meias Safa Jan 23 at 23:47
@MeiasSafa Use it instead of your code at the same place. – toscho Jan 24 at 3:17
Cant get it to work. Because i have 2 divs in the main page. one called navmain and the other navmain2 and i just wanna hide navmain2 if it is empty. – Meias Safa Jan 28 at 12:42
Its solved , thank you! – Meias Safa Jan 28 at 14:36

Solution nr 2:

Is to hide the div thru Js.

$('#jqm-home').live("pagecreate", function() {
$(".navmain2").each(function() {
if($(this).children("ul").length == 0) {
$(this).hide();
}
});
}); 
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.