Archived topic
Seperate Primary and Secondary Menu for Logged In vs Logged Out Users
11 replies · Started by Robert Cioffi on December 23, 2019
I'm using a function in a site specific plugin to do this with the primary menu. Here's the code:
function my_wp_nav_menu_args( $args = '' ) {
if( is_user_logged_in() ) {
$args['menu'] = 'Main';
} else {
$args['menu'] = 'Main2';
}
return $args;
}
add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' );`
But it messes up the secondary menu so I tried this:
function my_wp_nav_menu_args( $args = '' ) {
if( is_user_logged_in() ) {
$args['primary'] = 'Main';
$args['secondary'] = 'Header';
} else {
$args['primary'] = 'Main2';
$args['secondary'] = 'Header2';
}
return $args;
}
add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' );
But that only worked for the logged in users (I think).
Any way I can make this work?
Thanks.
Hi there,
What if we target the primary navigation?:
add_filter( 'wp_nav_menu_args', function( $args ) {
if ( 'primary' === $args->theme_location ) {
if ( is_user_logged_in() ) {
$args['menu'] = 'Main';
} else {
$args['menu'] = 'Main2';
}
}
return $args;
} );
Let me know :)
Did not work. Let me take a step back and explain what I'm trying to do. I have a primary nav and right below it a secondary nav. I'd like the logged in users to see one set of menus and the logged out users to see a different set. I named the menus Menu and Header for the logged in users and Menu2 and Header2 for the non logged in users.
I googled and found then followed this article by creating a function inside of a site specific plugin.
https://www.wpbeginner.com/wp-themes/how-to-show-different-menus-to-logged-in-users-in-wordpress/
Here's the plugin code I used:
<?php
/*
Plugin Name: Site Plugin for example.com
Description: Site specific code changes for example.com
*/
/* Start Adding Functions Below this Line */
function my_wp_nav_menu_args( $args = '' ) {
if( is_user_logged_in() ) {
$args['menu'] = 'Main';
} else {
$args['menu'] = 'Main2';
}
return $args;
}
add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' );
/* Stop Adding Functions Below this Line */
?>
It worked for the primary nav (Logged In> Main and Logged Out > Main2) but unfortunately it populates the secondary nave with Main & Main2. I need the secondary nav to be Logged In > Header and Logged Out > Header2.
There must be a way for this to work for both primary nav and secondary nav, yes?
Thanks for your help.
Does my code work with the primary navigation? You can target the secondary nav as well:
add_filter( 'wp_nav_menu_args', function( $args ) {
if ( 'primary' === $args['theme_location'] ) {
if ( is_user_logged_in() ) {
$args['menu'] = 'Main';
} else {
$args['menu'] = 'Main2';
}
}
if ( 'secondary' === $args['theme_location'] ) {
if ( is_user_logged_in() ) {
$args['menu'] = 'Header';
} else {
$args['menu'] = 'Header2';
}
}
return $args;
} );
I tried your code in the site specific plugin and the result was primary nav was Main and secondary nav was Header whether the user was logged in or logged out. Very puzzling.
Thanks.
Strange - can you try the edited code above?
That seems to be working. Thanks Tom!
You're welcome :)
@Tom is it possible to target the off-canvas menu location as well?
Absolutely, just use slideout instead of primary.
Sweet, I wasn't sure what to call it - thanks!
You're welcome :)