i am new to WordPress and trying to find a way to add menus based on user roles. I have (guest user or non logged in user) vendor and subscriber roles. I want to display different primary menus depending on what role user is. Example code taken from https://wpcodeus.com/display-different-wordpress-menu-to-logged-in-users/
Example code not working
function nav_menu_args( $args = '' ) {
if( is_user_logged_in('vendor')) {
( 'primary-menu' == $args['theme_location'] ) {
$args['menu'] = 'VendorMenu';
}
}
else if
( is_user_logged_in('subscriber')) {
( 'primary-menu' == $args['theme_location'] ) {
$args['menu'] = 'SubscriberMenu';
}
}
else
(!is_user_logged_in) {
( 'primary-menu' == $args['theme_location'] ) {
$args['menu'] = 'PrimaryMenu';
}
}
return $args;
}
add_filter( 'wp_nav_menu_args', 'nav_menu_args' );
At the moment it changes all menus to vendor menu including primary top and footer, all menus have the same menu.
Any help or guidance in the right direction will be much appreciated.
I am adding the code in to the custom/child theme functions.php file.
2
Answers
This worked for me (slightly different method but same outcome)
you can use current_user_can() function to check user capabiltiy or role. see this link.