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 have created one new menu and add bellow code in footer.php to display my menu

wp_nav_menu( array('menu' => 'footer-cn', 'menu_id' => 'nav', 'menu_class' => 'ft-nav' ));

Currently I did not add any pages inside this menu so I want to set some condition if any pages available inside footer-cn then display menu.

I want to set condition something this:

<?php
    if(){ // Some condition
        wp_nav_menu( array('menu' => 'footer-cn', 'menu_id' => 'nav', 'menu_class' => 'ft-nav' ));
    }
?>

Is it possible?

Thanks.

enter image description here

share|improve this question
    
Short answer is "yes - absolutely". If you edit your question to include the condition details, you'll likely have a response with how to accomplish the condition test. –  jdm2112 May 1 at 5:03

1 Answer 1

up vote 2 down vote accepted

No need to use conditional here to hide your nav menu when it is empty.

Have a look at wp_nav_menu( $args );. It accepts the following arguments

$defaults = array(
    'theme_location'  => '',
    'menu'            => '',
    'container'       => 'div',
    'container_class' => '',
    'container_id'    => '',
    'menu_class'      => 'menu',
    'menu_id'         => '',
    'echo'            => true,
    'fallback_cb'     => 'wp_page_menu',
    'before'          => '',
    'after'           => '',
    'link_before'     => '',
    'link_after'      => '',
    'items_wrap'      => '<ul id="%1$s" class="%2$s">%3$s</ul>',
    'depth'           => 0,
    'walker'          => ''
);

wp_nav_menu( $defaults );

The important one here is 'fallback_cb'

$fallback_cb (string) (optional)

If the menu doesn't exist, the fallback function to use. Set to false for no fallback. Note: Passes $args to the custom function.

Default: wp_page_menu

So to hide a menu when it is empty, you can simply just add this to your arguments

'fallback_cb' => false

So your end code should be

wp_nav_menu( array('menu' => 'footer-cn', 'menu_id' => 'nav', 'menu_class' => 'ft-nav',  'fallback_cb' => false));
share|improve this answer
1  
Excellent. Thank you so much. :) –  mlets3113858 May 1 at 8:41
    
My pleasure, glad I could help –  Pieter Goosen May 1 at 8:46

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.