Function and usage of is_nav_menu() in wordpress


Answers ( 1 )


    The is_nav_menu() function in WordPress is a conditional tag that determines whether a given ID, slug, name, or object represents a navigation menu. It's used primarily in theme development and plugins to check if a specific menu exists in WordPress.

    Here's a detailed explanation of its usage:

    Function Signature

    is_nav_menu( int|string|WP_Term $menu ): bool


    • Purpose: The is_nav_menu() function is used to verify if the specified $menu is indeed a navigation menu in WordPress.
    • Return Value: It returns true if the specified $menu is a valid navigation menu, and false otherwise.


    • $menu (int | string | WP_Term, required): This parameter can be the menu ID, slug, name, or a WP_Term object representing the menu. This flexibility allows for checking a menu in various ways depending on the available information.


    • Type: bool
    • Description: The function returns true if the $menu exists as a navigation menu in WordPress; otherwise, it returns false.

    Sample Usage

    Example 1: Checking by Menu ID

    if ( is_nav_menu( 5 ) ) {
        echo 'Menu with ID 5 exists.';
    } else {
        echo 'Menu with ID 5 does not exist.';

    In this example, 5 is the menu ID. The function checks if there's a menu with ID 5.

    Example 2: Checking by Menu Slug

    if ( is_nav_menu( 'main-menu' ) ) {
        echo 'Main menu exists.';
    } else {
        echo 'Main menu does not exist.';

    Here, 'main-menu' is the slug of the menu. The function checks if a menu with the slug 'main-menu' exists.

    Example 3: Checking by Menu Name

    if ( is_nav_menu( 'Top Navigation' ) ) {
        echo 'Top Navigation menu exists.';
    } else {
        echo 'Top Navigation menu does not exist.';

    This example uses the menu name 'Top Navigation' to check its existence.

    Example 4: Checking by WP_Term Object

    $menu_obj = wp_get_nav_menu_object( 'Footer Menu' );
    if ( is_nav_menu( $menu_obj ) ) {
        echo 'Footer Menu exists.';
    } else {
        echo 'Footer Menu does not exist.';

    In this case, the wp_get_nav_menu_object() function is used to get the WP_Term object of the 'Footer Menu', and then is_nav_menu() checks if this menu exists.

    Practical Application

    The is_nav_menu() function is particularly useful in theme development where the presence of specific menus might alter the layout or functionality. It ensures that operations involving menus are performed only if the specified menus are available, thereby preventing errors and enhancing the theme's robustness.

Leave an answer