- This topic has 11 replies, 2 voices, and was last updated 3 years, 9 months ago by Tom.
-
AuthorPosts
-
December 23, 2019 at 8:44 am #1112438Robert Cioffi
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.
December 23, 2019 at 7:14 pm #1112809TomLead DeveloperLead DeveloperHi 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 π
December 25, 2019 at 11:00 am #1113964Robert CioffiDid 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.
December 25, 2019 at 7:04 pm #1114090TomLead DeveloperLead DeveloperDoes 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; } );
December 26, 2019 at 3:01 am #1114238Robert CioffiI 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.
December 26, 2019 at 8:49 am #1114556TomLead DeveloperLead DeveloperStrange – can you try the edited code above?
December 26, 2019 at 9:17 am #1114587Robert CioffiThat seems to be working. Thanks Tom!
December 26, 2019 at 9:31 am #1114594TomLead DeveloperLead DeveloperYou’re welcome π
July 20, 2020 at 1:06 pm #1370077Arp@Tom is it possible to target the off-canvas menu location as well?
July 20, 2020 at 3:27 pm #1370172TomLead DeveloperLead DeveloperAbsolutely, just use
slideout
instead ofprimary
.July 21, 2020 at 10:26 am #1371126ArpSweet, I wasn’t sure what to call it – thanks!
July 21, 2020 at 3:13 pm #1371426TomLead DeveloperLead DeveloperYou’re welcome π
-
AuthorPosts
- You must be logged in to reply to this topic.