ID-100109807

I have always wondered if you can have different menus display on WordPress based on a user’s login status.  Back in the day when I was still willing to touch Joomla, I remember liking that specific Joomla feature very much.  As I haven’t really had the need for it up till now, I haven’t devoted any time towards figuring it out.

If you are reading this, it is assumed that you already know how to create custom menus through the WordPress admin panel.  If you are unsure how to do this, go and read this first.

This method will work on any WordPress installation, and is not limited to any specific template.   It is important to note that if you edit your template directly, and later update it, the changes you make now might be lost.  Rather create a Child Theme and edit that.

How?

Go to APPEARANCE –> MENUS and create two menus.  Give them simple names that will indicate which is which.  Mine is called ‘top-in’ and ‘top-out’.  It should be obvious which will be used where. For the ‘In’ menu, add all the pages / content / links that you want people to see once they are logged in on your site.  For the ‘Out’ menu, add the links you want them to see before they have logged in.  This could be links to register, etc.

To implement these different menus, we will be adding some code to the header.php file:

First, find this line of code:

<?php wp_nav_menu( array('container_class' => 'menu menu-top', 'theme_location' => 'menu_top','container' => 'div', 'fallback_cb' => false ) ); ?>

Edit and add the following, so that it looks like this:

<?php 
if( is_user_logged_in() ) {
 $menu = 'top-in';
} else {
 $menu = 'top-out';
}
wp_nav_menu( array( 'menu' => $menu, 'container_class' => 'menu menu-top', 'theme_location' => 'menu_top','container' => 'div', 'fallback_cb' => false ) ); ?>

What we are doing here is adding a parameter to wp_nav_menu to display a specific menu based on a user’s login status.