Using WordPress ‘is_nav_menu()’ PHP function

The is_nav_menu() WordPress PHP function determines whether the given ID is a navigation menu.

Usage

$is_nav_menu = is_nav_menu( $menu );

Custom example:

$main_menu = 'Main Menu';
if ( is_nav_menu( $main_menu ) ) {
    echo 'Main Menu is a navigation menu!';
} else {
    echo 'Main Menu is not a navigation menu.';
}

Parameters

  • $menu (int|string|WP_Term): Required. Menu ID, slug, name, or object of the menu to check.

More information

See WordPress Developer Resources: is_nav_menu()

Examples

Check by Menu ID

Function: Check if the menu with the given ID is a navigation menu.

$menu_id = 5;
if ( is_nav_menu( $menu_id ) ) {
    echo 'Menu with ID 5 is a navigation menu!';
} else {
    echo 'Menu with ID 5 is not a navigation menu.';
}

Check by Menu Slug

Function: Check if the menu with the given slug is a navigation menu.

$menu_slug = 'footer-menu';
if ( is_nav_menu( $menu_slug ) ) {
    echo 'Footer Menu is a navigation menu!';
} else {
    echo 'Footer Menu is not a navigation menu.';
}

Check by Menu Name

Function: Check if the menu with the given name is a navigation menu.

$menu_name = 'Header Menu';
if ( is_nav_menu( $menu_name ) ) {
    echo 'Header Menu is a navigation menu!';
} else {
    echo 'Header Menu is not a navigation menu.';
}

Check by WP_Term Object

Function: Check if the menu represented by the given WP_Term object is a navigation menu.

$menu_term = get_term( 5, 'nav_menu' );
if ( is_nav_menu( $menu_term ) ) {
    echo 'Menu with term ID 5 is a navigation menu!';
} else {
    echo 'Menu with term ID 5 is not a navigation menu.';
}

Check with a Non-Menu Item

Function: Check if a non-menu item is a navigation menu (expected to return false).

$non_menu_item = 'Not a menu';
if ( is_nav_menu( $non_menu_item ) ) {
    echo 'This is a navigation menu!';
} else {
    echo 'This is not a navigation menu.';
}