I’m building a website with wordpress and the Canvas Theme (from WooThemes).
I’m using the Top navigation for displaying items when a user is logged in.
When the user is not logged in, you can see only ‘become a member’ and ‘login’.
When the user logins, he/she sees another navigation with other menu items.
I thought of the following solution:
- register a new navigation.
- apply a hook which checks if a user is logged in or not
- According to the result, apply the navigation for registred users, otherwise show other.
I put this code in my functions.php:
add_action( 'init', 'register_top_menu_myisa', 10 );
function register_top_menu_myisa() {
$menus = array(
'top-menu-myisa' => __( 'Top Menu MyISA', 'woothemes' )
);
register_nav_menus( $menus );
}
add_action( 'woo_top', 'custom_top_navigation', 10 );
function custom_top_navigation() {
if ( function_exists( 'has_nav_menu' ) && has_nav_menu( 'top-menu' ) ) {
if(is_user_logged_in()) {
$top_menu = 'top-menu-myisa';
} else {
$top_menu = 'top-menu';
}
echo '<div id="top">';
echo '<div class="col-full">';
echo '<h3 class="top-menu">' . woo_get_menu_name( $top_menu ) . '</h3>';
wp_nav_menu( array( 'depth' => 6, 'sort_column' => 'menu_order', 'container' => 'ul', 'menu_id' => 'top-nav', 'menu_class' => 'nav top-navigation fl', 'theme_location' => $top_menu ) );
echo '</div>';
echo '</div>';
}
}
The first function registers the new navigation.
The second function contains the logic for deciding which top navigation should be displayed.
However: when I look at my site, it displays both menu’s (the one for non logged in users and the one for logged in users)
How can I modify the code so that it displays one top navigation menu, based on if the user is logged in or not?
Display Different Nav Menu For Logged in and Logged Out Users
Source: http://wpsites.net/web-design/members-nav-menu-logged-in-members/
I solved the problem myself. I don’t know the exact cause, but I’ll explain exactly what I did.
I was using WordPress 3.5. After I updated to 3.6, my navigation code started to work.
It’s also important to mention that I did use W3C Total Cache, but removed it (before applying this code), and set the
WP_DEBUG
flag tofalse
. I removed W3C Total Cache only because I was experimenting with it.This is the final code: