Login users display different WordPress menus

If the login user and the non login browser display different menus, the following code can be used:

the login user displays different WordPress menus

add the following code to the current topic function template functions.php Medium:

if( is_user_logged_in() ) { 
	$args['menu'] = 'logged-in';
} else { 
	$args['menu'] = 'logged-out';
} 
	return $args;
}
add_filter( 'wp_nav_menu_args', 'wpc_wp_nav_menu_args' );

After that, two new menus, logged in and logged out, are created respectively for the menus displayed by the login and ordinary visitors.

if the topic has multiple menus, you can display different menus in the specified menu position through the following code:

function wpc_wp_nav_menu_args( $args = '' ) {
if( is_user_logged_in()) { 
	if( 'top-navigation' == $args['theme_location'] ) { // Change top-navigation to theme specific name
		$args['menu'] = 'logged-in';
	}
} else { 
	if( 'top-navigation' == $args['theme_location'] ) { // Change top-navigation to theme specific name
		$args['menu'] = 'logged-out';
	}
}
	return $args;
}
add_filter( 'wp_nav_menu_args', 'wpc_wp_nav_menu_args' );

You can also use the above method to let different user roles display different menu contents.

original code: https://wpcodeus.com/display-different-wordpress-menu-to-logged-in-users/

Related Article
Two methods of automatically renaming uploaded files in WordPress

When we edit and publish articles, we often upload some pictures, audio and other files. Interested webmasters will find that Read more

WordPress tutorial: adding author filtering options to the background article list

When there are multiple users publishing articles in our site, in order to facilitate the administrator to view the article Read more

WordPress displays the registration time of the login user in the frontend

When using WordPress to build a website, some users want to display the user registration time in the frontend, but Read more

WordPress adds the number of SQL queries at the bottom

An excellent woodpress theme is definitely that the less SQL consulting is used, the better. How can we see how Read more

Leave a Reply

Your email address will not be published. Required fields are marked *

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-spam image