Conditional Menus

[ad_1]

Hi, I used the following PHP code to display different menus for Logged in/Not logged in users and it works fine for desktop users.

function my_wp_nav_menu_args( $args = ” ) {
if( is_user_logged_in() ) {
// Logged in menu to display
$args[‘menu’] = 43;
} else {
// Non-logged-in menu to display
$args[‘menu’] = 35;
}
return $args;
}
add_filter( ‘wp_nav_menu_args’, ‘my_wp_nav_menu_args’ );

However, I also want to have different menus for Logged in/Not logged in users on mobile. But I’m stuck at how to achieve this. I’ve been messing around with the code and have come up with the following:

function my_wp_nav_menu_args( $args = ” ) {
if( is_user_logged_in() ) {
// Logged in menu to display
if(is_mobile()) {
$args[‘menu’] = 9;
} else {
$args[‘menu’] = 2;
}
} else {
// Non-logged-in menu to display
$args[‘menu’] = 42;
}
return $args;
}
add_filter( ‘wp_nav_menu_args’, ‘my_wp_nav_menu_args’ );

I’m stuck because I don’t actually know if the “is\_mobile” function is correct, or how to implement the not-logged in mobile check. Any help/advice appreciated.

[ad_2]
2 Comments
  1. What are you trying to achieve with the first else condition?

    Assuming it’s unintentional, the conditional statements would look like

    `if( is_mobile() ) {`

    `$args[‘menu’] = 9;`

    `} else {`

    `// Non-logged-in menu to display`

    `$args[‘menu’] = 42;`

    `}`

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer