Check if a menu is empty?

wp_nav_menu has the argument fallback_cb, which is the function called if a menu doesn’t exist. This is set to wp_page_menu by default, which is why you see a list of pages if the menu doesn’t exist. If you explicitly set that to false, then nothing will be output if the menu doesn’t exist.

EDIT-

Given a menu name, you can load the menu object with wp_get_nav_menu_object. This will tell you if it exists, what its ID is (to pass as menu argument), and how many menu items it has.

$menu_name = get_post_meta( $post->ID, 'MenuName', true );
$menu = wp_get_nav_menu_object( $menu_name );
if( is_object( $menu ) ){
    echo 'This menu exists!';
    echo 'This menu has ' . $menu->count . ' menu items.';
    echo 'This menu ID is ' . $menu->term_id . '.';
} else {
    echo 'A menu with that name doesn\'t exist';
}

Leave a Comment