If you’re using WordPress as a membership site, you may want to display a special navigation menu to logged in members.
There’s different ways to do this using either CSS classes or PHP code.
Custom functions are the best way to go in my opinion because you don’t have to worry about adding more CSS to your child themes files and slowing down your site. PHP code is more powerful and efficient. You also don’t need to worry about cross browser issues.
To display a different nav menu for logged in and logged out users, you firstly need to create 2 menu’s.
I won’t show you how to do that because its so easy even your mum could do it!
- One for Logged in members
- And one for logged out visitors
Here’s what they look like once you’ve created them.
You can then select the non-members menu as the primary menu and leave the members menu unassigned.
You’d probably want to include a registration link in the non members nav menu but not in the members menu.
Paste Code In Functions File
This is the code you need to paste into your child themes functions.php file.
You only need the first code snippet above if you are using the same menu location for logged in members and logged out non members.
If you want to display another menu for members in a different menu location, you’ll need to use both code embeds above together and change the menu location in the code which is currently set to display the secondary menu for logged in members.
Different Menu Links For Logged In Members
Another way to display a different menu for logged in members is to include a custom menu link item which is added to the primary or secondary nav menu for logged in members. When logged out, the link is not added to the menu.
Replace the url in this code with your own menu link and change the primary to secondary if needed.
Here’s some code which you can use to display the nav menu for logged in members in a different location like the footer for either the primary or secondary nav menu’s.
Here’s a plugin which pretty much does the same thing as the code snippets above.
This plugin enables you to hide menu items based on user roles. You can:
- Limit display of this menu item to users who are logged in or out, or customize by role.
- Limit display of this menu item to users of selected roles.
The conditional tag to hide menu items for logged in users is:
That’s all folks!
I know there are other ways to display different menus for logged in and logged out users however these are the best in my opinion.
How about you? Do you have a better solution than this one?