- This topic has 17 replies, 5 voices, and was last updated 1 year, 6 months ago by
Ying.
-
AuthorPosts
-
October 26, 2016 at 8:34 am #239055
kvejonsio
Hi Tom
I have added the code presented underneath in to generatepress-child -> functions.php, also have added the classes directly in the snippet, it works, but would you like to look on it and tell me if it’s a good solution, or maybe you know something better.
add_filter( 'wp_nav_menu_items', 'add_loginout_link', 10, 2 ); function add_loginout_link( $items, $args ) { if (is_user_logged_in() && $args->theme_location == 'primary') { $items .= '<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children"><a href="'. wp_logout_url( get_permalink( wc_get_page_id( 'myaccount' ) ) ) .'">Log Out<span role="button" class="dropdown-menu-toggle" aria-expanded="false"></span></a> <ul class="sub-menu"><li class="menu-item menu-item-type-post_type menu-item-object-page"><a href="' . get_permalink( wc_get_page_id( 'myaccount' ) ) . '">My account</a></li></ul> </li>'; } elseif (!is_user_logged_in() && $args->theme_location == 'primary') { $items .= '<li><a href="' . get_permalink( wc_get_page_id( 'myaccount' ) ) . '">Log In</a </li>'; } return $items; } return $items; }
October 26, 2016 at 9:18 am #239073Tom
Lead DeveloperLead DeveloperLooks good to me!
For anyone else, the above function has WooCommerce specific functions (wc_get_page_id), so if you’re not using WooCommerce and use this code, you’ll get a fatal error.
If you are using WooCommerce, it should work perfectly 🙂
October 26, 2016 at 10:46 am #239106kvejonsio
yes, sorry I forgot to mention it, that code is for generatepress + woocommerce.
Thanks a lot Tom
October 26, 2016 at 10:46 am #239107Tom
Lead DeveloperLead DeveloperThanks for sharing it 🙂
October 30, 2016 at 3:14 pm #240159Roman
Hi there,
just for your interest. If you add/modify the if-conditionif ($args->theme_location == 'primary' || $args->theme_location == 'slideout')
your login/logout should work also in slidout-menu.
My question is: How can i make the Login/Logout appear on the right side of the primary-desktop-menu and make it appear on top of slideout-menu? As an perfect alternative, a behavior like the integrated search-icon would be nice.
October 30, 2016 at 9:06 pm #240220Tom
Lead DeveloperLead DeveloperYou can float it to the right like this: https://generatepress.com/knowledgebase/menu-social-icons/
Not too sure what you mean by on top of the slideout menu? You can add it as the first menu item in the slideout menu?
September 28, 2020 at 1:09 pm #1462811roadlink
I added this snipped as below and it is working great.
But this item can’t be shown in menu settings, so I can’t customize it or make it float right.
I use Prime from site library which already has login | register link in the primary menu.
but can’t change it like thisadd_filter( 'wp_nav_menu_items', 'add_loginout_link', 10, 2 ); function add_loginout_link( $items, $args ) { if (is_user_logged_in() && $args->theme_location == 'primary') { $items .= '<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children"><li class="menu-item menu-item-type-post_type menu-item-object-page"><a href="' . get_permalink( wc_get_page_id( 'myaccount' ) ) . '">Hesabım</a></li> </li>'; } elseif (!is_user_logged_in() && $args->theme_location == 'primary') { $items .= '<li><a href="' . get_permalink( wc_get_page_id( 'myaccount' ) ) . '">GiriÅŸ Yap</a </li>'; } return $items; }
September 29, 2020 at 3:15 am #1463429David
StaffCustomer SupportHi there,
you can include your own custom CSS Class within the markup to target that for styling.
For example – this line:<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children">
You can include your won class eg.
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children"><li class="menu-item menu-item-type-post_type menu-item-object-page YOUR-CUSTOM-CSS-CLASS">
If you want to try that – happy to help with any CSS if you can share a link to the site.
September 1, 2021 at 2:13 am #1915042Alec
Hello,
I have placed the code from above https://bit.ly/3Btd7zU in my child’s function.php and it works fine (except that the last two lines show an error, I had to remove them:
return $items;}
The question is: how can we enable this same dynamic Log-in log-out showing on mobile slide-out (off-canvas in my case) menu?
Here is the URL: https://aleckatzmusic.comThanks in advance
September 1, 2021 at 4:08 am #1915141David
StaffCustomer SupportHi there,
this line in the code sets the condition to the primary menu:
if (is_user_logged_in() && $args->theme_location == 'primary') {
To include the Offcanvas (slideout) you would change it to:
if (is_user_logged_in() && ( $args->theme_location == 'primary' || $args['theme_location'] == 'slideout' ) ) {
September 1, 2021 at 4:23 am #1915162Alec
I get this error msg (line 212 is the line: if (is_user_logged_in() && $args->theme_location == ‘primary’) {
that I change:Your PHP code changes were rolled back due to an error on line 212 of file wp-content/themes/generatepress_child/functions.php. Please fix and try saving again.
syntax error, unexpected ”theme_location” (T_CONSTANT_ENCAPSED_STRING), expecting identifier (T_STRING) or variable (T_VARIABLE) or ‘{‘ or ‘$’
forgive my lack of PHP knowledge 🙂
September 1, 2021 at 5:53 am #1915243David
StaffCustomer SupportOops – sorry about that, i updated the line above to include the missing
)
September 1, 2021 at 2:11 pm #1915927Alec
Sorry, but there is a problem with this, I get an error, and also it stays “log-out” in the menu even if user is not logged-in.
I tried and this one from here https://bit.ly/3Btd7zU
combined with this line:
if ($args->theme_location == 'primary' || $args->theme_location == 'slideout')
works the best:
/*Log-in Log-out in primary & slide-out menu*/ add_filter( 'wp_nav_menu_items', 'add_loginout_link', 10, 2 ); function add_loginout_link( $items, $args ) { if ($args->theme_location == 'primary' || $args->theme_location == 'slideout') { $items .= '<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children"><li class="menu-item menu-item-type-post_type menu-item-object-page"><a href="' . get_permalink( wc_get_page_id( 'myaccount' ) ) . '">my account</a></li> </li>'; } elseif (!is_user_logged_in() && $args->theme_location == 'primary') { $items .= '<li><a href="' . get_permalink( wc_get_page_id( 'myaccount' ) ) . '">Log-in</a </li>'; } return $items; }
September 2, 2021 at 5:31 am #1916423David
StaffCustomer SupportGlad to hear you found a solution
March 8, 2022 at 1:09 pm #2147256Daniel
Hi there!
I was looking for a solution to rename the My Account link in GeneratePress if I’m logged out.
I’ve used a custom top bar in Elements hooked to the before_header hook. Then I’ve used GenerateBlocks buttons to create a custom My account button with the user icon, so I’m not using a WP menu.
Is it possible to change that GenerateBlocks button text in a similar way if user is logged out?
-
AuthorPosts
- You must be logged in to reply to this topic.