Using Conditional logic in menus in WordPress

The current method to handle menus is to go to the Appearance -> Menus. Create a menu, add content and set the display location. Then Save the menu. Add a few menus with different content before moving on to the below conditional examples.

WordPress Appearance Menus screen
WordPress Appearance > Menus screen

What follows are some examples of using If and Else in WordPress to create a conditional.

One menu for logged in another menu for not logged in users

Create two menus. One for logged in users and another for not logged in users.
Add this code to a code snippet plugin or a child theme functions file.
The names used: Sub Menu and Top Menu are the names I gave the menus I created.

function my_wp_nav_menu_args( $args = '' ) {
    if( is_user_logged_in() ) {
        $args['menu'] = 'Sub Menu';
    } else {
        $args['menu'] = 'Top Menu';
    }
    return $args;
}
add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' );

If page show specific menu. If blog or post show another menu.

Using the same logic as in the above code I changed the if statement like so.
If page then show menu Top Menu. If Blog or posts show menu Sub Menu.

function my_wp_nav_menu_args( $args = '' ) {
    if( is_page() ) {
        $args['menu'] = 'Top Menu';
    } else {
        $args['menu'] = 'Sub Menu';
    }
    return $args;
}
add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' );

If page Products show Product Menu. Elseif front page show Top menu. Else show Sub menu.

I made a new menu and called it Product Menu and added pages to it.
I have created a If is_page Product to show Product Menu.
Elseif is_front_page show Top Menu.
Else (if none of the above) show the Sub Menu.

function my_wp_nav_menu_args( $args = '' ) {
    if( is_page("Product") ) {
        $args['menu'] = 'Product Menu';
    } elseif ( is_front_page() ) {
        $args['menu'] = 'Top Menu';
    } else {
        $args['menu'] = 'Sub Menu';	
    }
    return $args;
}
add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' );

Using and / &&

One can have many elseif statements as well as using && such as

elseif ( is_front_page() ) &&  if( !is_user_logged_in() {

The above says elseif is the front page and is ! (not) logged in then do something.

Conditional statements in WordPress

! = is not.
&& = and
&& is_page();
&& is_page (‘id of page’)
&& is_page (‘about-me’)
&& is_page ( array(‘member’, ‘member-page-2’, ‘member-page-3’, ‘member-page-4’) )
&& ( is_archive() || is_singular(‘page’) ) ) —> II brings two options together.
&& is_page_template( ‘name of page template’ )
&& is_user_logged_in() )
Here is a much broader list: https://codex.wordpress.org/Conditional_Tags

Additional Resources

https://developer.wordpress.org/reference/functions/is_page/
https://stackoverflow.com/questions/12130840/how-to-use-if-if-else-and-else-in-wordpress
https://wplearninglab.com/how-to-create-conditional-menus-in-wordpress/
https://stackoverflow.com/questions/63096571/conditional-menu-in-wordpress-via-functions-php-no-plugin

Share the article:

Leave a Reply

Your email address will not be published. Required fields are marked *