Show Different Navigation Menu to your Members and Non-members

This tutorial will explain how you can show different navigation menus to your logged in members and non-members if the theme supports WordPress menu.

General Warning: This is more of a General WordPress theme tweaking tutorial as the menu gets displayed by your theme. eMember has nothing to do with displaying your navigation menu. Different themes display the navigation menu differently and from different place(s) of the template file, so this tutorial may not be applicable to all themes. Also, a little bit of HTML and PHP knowledge is required for this tutorial.

If your theme has an option where it allows you to show different menu to logged in users and non-logged in users then you do not need to follow this tutorial.

Summary of What We will Do

  1. We will create two WordPress navigation menus from the “Appearance -> Menus” section of WordPress. One for the “logged in members” and the other for the “non logged in members”.
  2. We will then change your theme’s code (where the menu is getting displayed from) and replace the existing menu display code with a new dynamic menu display code.

Creating the Menus

Step 1: Go to the “Appearance -> Menus” section in your WordPress dashboard and create a new menu with the name “logged-in-members” (we want to show this menu to our logged in members)

Creating a New WordPress Menu for Members

Step 2: Add whatever menu items you want to show to your logged in members in this newly created menu and then save this menu.

Adding Menu Items to Your Newly Created Menu

Step 3: Now, create another menu with the name “normal-visitor-menu“. we want to show this menu to our non logged in members (anyone who is not logged into the site will see this menu).

Create another menu for the normal visitors

Step 4: Add whatever menu items you want to show to your normal visitors in this newly created menu and then save this menu.

That gives you two WordPress menus (one for the members and one for everyone else).

Utilizing the Newly Created Menus in Your Theme

Step 5: Find the block of code in your theme that is displaying your navigation menu (you can ask your theme developer where this line of code is). Generally the navigation menu code resides in the “header.php” template file of your theme.

Example 1: The following line of code in the “header.php” file outputs the navigation menu in the WordPress Twenty Ten theme:

<?php wp_nav_menu( array( 'container_class' => 'menu-header', 'theme_location' => 'primary' ) ); ?>

Example 2: The following line of code in the “header.php” file outputs the navigation menu in the WordPress Twenty Eleven theme:

<?php wp_nav_menu( array( 'theme_location' => 'primary' ) ); ?>

Step 6: Once you have identified the block of code that is displaying your navigation menu, simply replace that code with the following block of code which will output the menus we already created dynamically:

<?php
if ( wp_emember_is_member_logged_in() ) {
wp_nav_menu( array( 'menu' => 'logged-in-members' ) );
} else {
wp_nav_menu( array( 'menu' => 'normal-visitor-menu' ) );
}
?>

WordPress Twenty Ten Theme: Use the following block of code for step 6 if you are using the WordPress Twenty Ten theme:

<?php
if ( wp_emember_is_member_logged_in() ) {
    wp_nav_menu( array( 'menu' => 'logged-in-members', 'container_class' => 'menu-header' ));
} 
else {
    wp_nav_menu( array( 'menu' => 'normal-visitor-menu', 'container_class' => 'menu-header' ));
}
?>

Show Different Menus to Different Membership Levels

If you want to show totally different menus to members of different membership level then you can do that too. Create your menus as usual then use the following logic:

<?php
if ( wp_emember_is_member_logged_in('1') ) {//Show this menu to members of membership level 1
    wp_nav_menu( array( 'menu' => 'level-1-menu', 'container_class' => 'menu-header' ));
} 
else if ( wp_emember_is_member_logged_in('2') ) {//Show this menu to members of membership level 2
    wp_nav_menu( array( 'menu' => 'level-2-menu', 'container_class' => 'menu-header' ));
} 
else if ( wp_emember_is_member_logged_in('3') ) {//Show this menu to members of membership level 3
    wp_nav_menu( array( 'menu' => 'level-3-menu', 'container_class' => 'menu-header' ));
}
else {//Show this menu to non logged in visitors
    wp_nav_menu( array( 'menu' => 'normal-visitor-menu', 'container_class' => 'menu-header' ));
}
?>
This entry was posted in Additional Resources and tagged , . Bookmark the permalink.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>