Are you building a website that requires users to logged in to access it? Then, you’ll likely need a dynamic navigation menu that displays different menu items to users depending on whether they’re logged in or not. In this article, we’ll show you how to display custom menus to logged in users.
Instructions:
Add this snippet to the template file where you want to display your navigation menu.
<?php if ( is_user_logged_in() ) { wp_nav_menu( array( 'theme_location' => 'logged-in-menu' ) ); } else { wp_nav_menu( array( 'theme_location' => 'logged-out-menu' ) ); } ?>
Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.
If you liked this code snippet, please consider checking out our other articles on the site like: Best WordPress Page Builders (Compared).
How do you add this to a theme? The above code shows nothing in the navigation menu
[…] I’ve been in the process of building an online community for the Utah Grotto of the National Speleological Society and ran into a wall when it comes to creating a different menu for logged in and non logged in users. The primary problem was in the fact that several of the buddypress supported themes only support one menu and you need at least two in order for the is_user_logged_in() function to work properly. So here was the solution I came up with after referencing the following sites: http://wpsnipp.com/index.php/theme/display-different-menus-for-logged-in-users/ and http://smartcoderszone.com/2010/07/how-to-add-multiple-menu-in-wordpress/. […]
AWESOME! Thank you a billion times!
Np Erwin, glad that I could help.
Thank you – I’ll give this a try 🙂
Just found this site and I think it is going to be invaluable to me.
Thank you.
Hi Janice glad to hear you like wpsnipp, hope that you enjoy all the snippets.