Swap menus in sidebar if user is logged in or logged out
-
Hi there
I’m trying to add a function in my functions.php of my childtheme to switch between to different side-navigations.
If a user is logged in I want to display a special menu in my sidebar (set as widget in the backend).
If he’s logged out I only want to display a login link.I registered a second menu location:
function register_my_menu() { register_nav_menu( 'sidebar-menu', __( 'Private Menu' ) ); } add_action( 'init', 'register_my_menu' );
And now I am trying to switch the menu:
function my_wp_nav_menu_args( $args = '' ) { if( is_user_logged_in() ) { $args['theme_location'] = 'sidebar-menu'; $args['menu'] = 'private-menu'; } else { $args['theme_location'] = 'sidebar-menu'; $args['menu'] = 'login-menu'; } return $args; } add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' );
Unfortunately this does not work. It actually also switches my primary menu.
Is there any way how to target only my menu at my sidebar theme location?PS: I did create those two menus in the admin area. The two names are the menu’s slugs.
And I cannot use any plugins.Thanks for any hint on that subject…
Viewing 6 replies - 1 through 6 (of 6 total)
Viewing 6 replies - 1 through 6 (of 6 total)
- The topic ‘Swap menus in sidebar if user is logged in or logged out’ is closed to new replies.