I'm using the below code to display two different menus in my storefront based child theme based on a users logged in status. But When I have a secondary menu (links by the search bar) it is replaced with the primary menu. Is there a way to differentiate between the two when checking for logged in users?
The below code is in my functions.php file.
//Custom Nav Menu
function custom_wp_nav_menu_args( $args = '' ) {
if( is_user_logged_in() ) {
$args['menu'] = 'logged-in';
}else {
$args['menu'] = 'logged-out';
}
return $args;
}
add_filter( 'wp_nav_menu_args', 'custom_wp_nav_menu_args' );
The theme is the default storefront theme, which should displays the secondary menu to the left of the search box. As you can see here its displaying (either) the logged-in or logged-out menu.
Please see Selected answer for more information. My Working Code...
//Custom Nav Menu
function custom_wp_nav_menu_args($args) {
if ( isset($args['theme_location']) && 'primary' === $args['theme_location'] ) {
// do something
if( is_user_logged_in() ) {
$args['menu'] = 'logged-in';
}else {
$args['menu'] = 'logged-out';
}
}
return $args;
}
add_filter( 'wp_nav_menu_args', 'custom_wp_nav_menu_args' );