Black Friday Sale! Get up to $30 off GP Premium! Learn more ➝

[Resolved] Menus, Menus and More Menus

Home Forums Support [Resolved] Menus, Menus and More Menus

Home Forums Support Menus, Menus and More Menus

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #219010
    Mary Pearson

    Hi Tom,

    I don’t know if you can help with this or not. I have different menus for my logged-in users and my logged-out users by adding the following in my functions.php

    /* Begin - Shows different Menus for logged in and logged out  */
    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' );
    	
    function wp_username_in_nav( $menu ){
        global $current_user;		
        return $menu = str_replace('--sububsername--', $current_user->user_login, $menu );
    }
    add_filter( 'wp_nav_menu', 'wp_username_in_nav' );
    /* End - Shows different Menus for logged in and logged out  */

    All works great except that now I have expanded the site with sub-domains and I find that I really need that secondary menu, however, using the code above, when I try to use a secondary menu I get a duplicate of the main menu. If I remove the code above then the secondary menu works as it should but I lose my logged in, logged out function.

    Is there a way to keep my logged-out and logged-in menus (primary menu) and also have a secondary menu with different links?

    Thanks!

    #219084
    Tom
    Lead Developer
    Lead Developer

    This is more of a question for stackoverflow as it’s not directly GeneratePress related.

    Basically, you only want to apply it to the primary theme location, so you need to wrap your args in something like this:

    if ( $args->theme_location == 'primary' ) {
        if( is_user_logged_in() ) {	
    	    $args['menu'] = 'logged-in';  
    	} else {
    	    $args['menu'] = 'logged-out';  
    	}
    }

    You seem to be getting pretty deep into WP development, I think you would find stackoverflow to be incredibly helpful if you learn how to use it to its potential πŸ™‚

    Hope this helps!

    #219328
    Mary Pearson

    Oh Tom, you give me way too much credit. I’ve been on stackoverflow for a few things and can never understand a word of what I’m reading.

    Thank you for the help. I will give it a try and let you know.

    #219344
    Tom
    Lead Developer
    Lead Developer
    #219816
    Mary Pearson

    I couldn’t get the code to work for me but I found a plugin that does the same thing. It’s called nav-menu-roles.

    Just thought I’d let you know in case anyone else is trying to do the same thing.

    Thanks for your help Tom!

    #219838
    Tom
    Lead Developer
    Lead Developer

    Glad you found a solution πŸ™‚

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