- This topic has 5 replies, 2 voices, and was last updated 6 years, 9 months ago by
Tom.
-
AuthorPosts
-
August 17, 2016 at 2:05 pm #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!
August 18, 2016 at 12:11 am #219084Tom
Lead DeveloperLead DeveloperThis 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!
August 18, 2016 at 1:54 pm #219328Mary 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.
August 18, 2016 at 2:28 pm #219344Tom
Lead DeveloperLead DeveloperGlad I could help π
August 20, 2016 at 6:47 am #219816Mary 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!
August 20, 2016 at 8:55 am #219838Tom
Lead DeveloperLead DeveloperGlad you found a solution π
-
AuthorPosts
- You must be logged in to reply to this topic.