If you're building a website using WordPress which requires users to login to the site, then you will most likely need to have a dynamic navigation menu, which displays different menu items to users if they are logged out, logged in or have a specific user role.
This is because it makes no sense to show a register or login link to users who have already logged in or provide a link to a user's profile when they are a visitor and do not have a profile on your website. In this article, we will show you how to show different menus to logged in users in WordPress.
Creating Multiple Menus in WordPress
In WordPress even if your theme has one menu location, you can still create multiple menus for the same location.
Go to Appearance » Menus, create two menus logged-in and logged-out.
After creating the menus, add this code in your theme's functions.php file (if you are using WooVina Child Themes, then you need follow this to know how to add the custom PHP code):
function my_wp_nav_menu_args($args = '') {
if(is_user_logged_in()) {
$args['menu'] = 'logged-in';
}
else {
$args['menu'] = 'logged-out';
}
return $args;
}
add_filter('wp_nav_menu_args', 'my_wp_nav_menu_args');
That's all you will see that your logged in visitors will see the logged-in menu and non-registered or logged out users will see a different menu.
This method allows you to create two different menus for your users so that you can freely update your menus for logged in or logged out users. There are other ways to do the same thing. For example, if you just want to add login and logout links to your navigation menu then you should add custom menu items in WordPress navigation menu.
We hope this article helped you show different menus to logged in users in your WordPress site. For questions and feedback please contact us here.