1

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

  • 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 '14 at 5:03
2

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));
  • My pleasure, glad I could help – Pieter Goosen May 1 '14 at 8:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.