[Resolved] Secondary Menu in sidebar and conditional logic (logged in/ logged out)

Home Forums Support [Resolved] Secondary Menu in sidebar and conditional logic (logged in/ logged out)

Home Forums Support Secondary Menu in sidebar and conditional logic (logged in/ logged out)

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #609081
    Patricia

    I have three menus: Logged In, Logged Out (for Primary Menu Location), and a User Dashboard Menu (set as Secondary menu location and located in the left sidebar). I used the following two code snippets to designate logged in vs. logged out for primary and to hide the sidebar for non logged in users. But my problem is that now the sidebar only displays the primary menu, not secondary menu. I’m sure I’ve done something wrong in the coding but can’t place it.

    Here it is:

    1.LOGGED IN OUT PRIMARY MENU

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

    2. HIDE SIDEBAR FROM LOGGED OUT USER

    add_filter( 'generate_sidebar_layout','tu_logged_out_sidebar' );
    function tu_logged_out_sidebar( $layout )
    {
     	if ( ! is_user_logged_in() ) {
                return 'no-sidebar';
            }
    
     	// Or else, set the regular layout
     	return $layout;
    }
    GeneratePress 2.1.2
    #609453
    Leo
    Staff
    Customer Support

    Hi there,

    Is there a specific page I should be looking at?

    Currently the two pages I see are all from Elementor.

    Let me know ๐Ÿ™‚

    #609721
    Patricia

    Leo,

    Well you won’t see the left sidebar menu unless you are logged in. That part of the code is working fine. Why it displays the logged in primary menu is where I’m confused.

    I originally had this code in a child theme file. But that didn’t seem to work at all. So, now you can find them in the code snippets plugin. You’ll see the three menus I have in the appearance>menu section. I designated the User Dashboard for the secondary menu, which was working fine until I added the conditional display rules. I gave you admin permissions.

    #609986
    Tom
    Lead Developer
    Lead Developer

    You may need to target the theme location of the menu you want to alter.

    So if this code is targeting the primary menu, your code should look like this:

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

    Tom,

    Thanks that was it! I just needed to target the specific theme menu location. Your code was perfect.

    #610104
    Tom
    Lead Developer
    Lead Developer

    Awesome, glad I could help! ๐Ÿ™‚

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.