Show Different Menu Items To Logged in Users

If you run a membership site on WordPress or if you have some private pages that are visible only to registered users, then you may want to show some menu items only to logged in users.

You can easily show different menu items to logged in users in WordPress by creating two different custom menus. One for users who are logged in, and other for those who are not.

To show different menus to logged in users and users who are not logged in, first create two different custom menus. Name them logged-in-menu and logged-out-menu for brevity.

And obviously, for logged-in-menu menu, add the menu items that you want to display to the users who are logged in. Similarly for for logged-out-menu menu, add the menu items that you want to display to the users who are not logged in.

Now, open the file which contain the code to display menu. Usually it is header.php. Now modify the wp_nav_menu code to display different menus depending upon whether the user is logged in by using the is_user_logged_in condition.

For example, in TwentyTen theme, the code to display menu in header.php is this,

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

Modify this code like this,

<?php
if( is_user_logged_in() ) {
    $menu = 'logged-in-menu';
} else {
    $menu = 'logged-out-menu';
}
 
wp_nav_menu( array( 'menu' => $menu, 'container_class' => 'menu-header', 'theme_location' => 'primary' ) );
?>

Now TwentyTen theme will display a different menu depending upon whether a user is logged in to your WordPress website or not. Apply the same trick to your theme.

Comment?

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