WordPress.org

Ready to get started?Download WordPress

Forums

wp_nav_menu and conditional nav menu (1 post)

  1. Wesley
    Member
    Posted 8 months ago #

    I have a place in my template that I want to display a specific menu, if it exists, and otherwise not show anything, even if other menus exist.

    In my functions.php I have this code:

    add_action('init', 'register_custom_menu');
    function register_custom_menu() {
    	register_nav_menu('navigation', __('Navigation', 'framework'));
    }

    In the template I have this code to display that menu:

    <?php wp_nav_menu(array(
    	'menu' => 'navigation',
    	'container' => 'nav',
    	'container_id' => 'top-nav',
    	'fallback_cb' => 'false',
    	'depth' => -1
    )); ?>

    What currently happens: I have two menus, one named "Navigation" and one named "Test". "Navigation" shows where the above code is in the template, and "Test" is in a widget in the sidebar. If I delete the menu named "Navigation" then the "Test" menu displays where "Navigation" was and also in the widget. What I want is for nothing to display in the "Navigation" spot unless there is a menu named "Navigation".

    I thought by setting fallback_cb to false, if the menu named "Navigation" doesn't exist, then it will fallback to nothing but that doesn't seem to be the case. I know there's probably a way to use an if statement to test if the menu exists, but I was hoping for something more elegant. Am I misunderstanding how wp_nav_menu() works or is there something else I'm missing?

    Thanks for your help in advance.

Reply

You must log in to post.

About this Topic