• Resolved jamesdio

    (@jamesdio)


    I need have different menus for logged users, but I have a three menus:

    logged-in-menu
    logged-out-menu
    for-all-users-menu

    I put this code into function.php:

    function my_wp_nav_menu_args( $args = '' ) {
    
    if( is_user_logged_in() ) {
    	$args['menu'] = 'logged-in-menu';
    } else {
    	$args['menu'] = 'logged-out-menu';
    }
    	return $args;
    }
    
    add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' );

    But I can’t see third menu – for-all-users-menu in a footer. Istead of this I see:
    – logged-in-menu when I am logged in or
    – logged-out-menu when I am logged out

    When I remove above code from functions.php the third menu is properly in a footer.

    So the question is: what should I do in order to see the third menu for all users (never mind logged or not).

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter jamesdio

    (@jamesdio)

    Any idea?

    Thread Starter jamesdio

    (@jamesdio)

    OK. So I will answer for others…

    First of all, edit functions.php and add this code:

    function register_my_menus() {
      register_nav_menus(
        array(
          'menu-footer' => __( 'Menu in the footer' ),
        )
      );
    }
    add_action( 'init', 'register_my_menus' );

    The code above registers new menu, name of the new menu is in my case: menu-footer

    Next add these lines:

    function my_wp_nav_menu_args( $args = '' ) {
    
    // Primary menu location
    if( 'primary' == $args['theme_location'] ) {
    
    if( is_user_logged_in() ) {
    $args['menu'] = 'logged-in-menu';
    } else {
    $args['menu'] = 'logged-out-menu';
    }
    return $args;
    }
    
    // Secondary menu location
    if( 'menu-footer' == $args['theme_location'] ) {
    
    if( is_user_logged_in() ) {
    $args['menu'] = 'menu-informacyjne';
    } else {
    $args['menu'] = 'menu-informacyjne';
    }
    return $args;
    }
    }
    add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' );

    The first menu (primary) is different for logged in/logged out users.
    The second menu (menu-footer) is always visible for logged/non logged users.

    I edited one more file: footer.php in my theme folder and added this line:
    <?php wp_nav_menu( array( 'theme_location' => 'menu-footer' ) ); // this is my menu-footer>

    Now everything works fine. All edited files are in child theme folder of course…

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Different menus for logged users’ is closed to new replies.