How to create different navigation for registered users in WooThemes Canvas?

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.

Read More

I thought of the following solution:

  1. register a new navigation.
  2. apply a hook which checks if a user is logged in or not
  3. 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)
Two navigation menu's displayed

How can I modify the code so that it displays one top navigation menu, based on if the user is logged in or not?

Related posts

2 comments

  1. add_action('init','wpsites_members_menu');
    
    function wpsites_members_menu(){
    if(is_user_logged_in()){
        add_filter( 'wp_nav_menu_args' , 'logged_in_nav_menu' );
    }
    }
    

    Display Different Nav Menu For Logged in and Logged Out Users

    Source: http://wpsites.net/web-design/members-nav-menu-logged-in-members/

    function logged_in_nav_menu( $args ) {
    if ( $args['theme_location'] == 'primary' ) { 
        $args['menu'] = 'members'; 
    }
    return $args;
    }
    
  2. 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 to false. I removed W3C Total Cache only because I was experimenting with it.

    This is the final code:

    /* = Register the MyISA top navigation.
    ------------------------------------------------------------------------------------ */
    
    add_action( 'init', 'isa_register_top_menu_myisa');
    
    function isa_register_top_menu_myisa() {
        if(function_exists('has_nav_menu') && !has_nav_menu('top-menu-myisa')) {
        $menus = array(
                    'top-menu-myisa' => __('Top Menu MyISA', 'woothemes')
                );
        register_nav_menus($menus);
        }
    }
    
    
    /* = Determine the top navigation to display.
    
    When a user is logged in, the MyISA top navigation is displayed.
    Otherwise the default top navigation wil be visible.
    ------------------------------------------------------------------------------------ */
    
    add_action('woo_top', 'woo_top_navigation');
    
    function woo_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>';
        }
    }
    

Comments are closed.