将登录/注销添加到菜单 Woocommerce Wordpress
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15179863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add login/logout to menu Woocommerce Wordpress
提问by SilverNightaFall
I am trying to figure out how to add a login/logout to the menu. When I add this code to the wordpress header the content and sidebar disappear. How can I add the login/logout to the menu without losing the rest of my page. I have tried adding it in the settings menu and it doesn't work with the theme I'm using.
我想弄清楚如何在菜单中添加登录/注销。当我将此代码添加到 wordpress 标题时,内容和侧边栏消失了。如何在不丢失页面其余部分的情况下将登录/注销添加到菜单中。我尝试将它添加到设置菜单中,但它不适用于我正在使用的主题。
<ul>
<?php
$myaccount_page_id = get_option( 'woocommerce_myaccount_page_id' );
if ( $myaccount_page_id ?&& !is_user_logged_in()) {
$myaccount_page_url = get_permalink( $myaccount_page_id );
?>
<li><a href="<?php echo $myaccount_page_url; ?>" class="login-header"><?php _e('Login', 'woocommerce'); ?></a></li>
<?php
}
$myaccount_page_id = get_option( 'woocommerce_myaccount_page_id' );
if ( $myaccount_page_id && is_user_logged_in()) {
$logout_url = wp_logout_url( get_permalink( $myaccount_page_id ) );
if ( get_option( 'woocommerce_force_ssl_checkout' ) == 'yes' )
$logout_url = str_replace( 'http:', 'https:', $logout_url );
?>
<li><a href="<?php echo $logout_url; ?>" class="login-header"><?php _e('Logout', 'woocommerce'); ?></a></li>
<?php } ?>
<li><a href="<?php echo $woocommerce->cart->get_cart_url(); ?>" class="cart-header"><?php _e('Shopping Cart', 'woocommerce'); ?> <?php echo sprintf(_n('(%d)', '(%d)', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?></a></li>
<li><a href="<?php echo $woocommerce->cart->get_checkout_url(); ?>" class="check-header"><?php _e('Checkout', 'woocommerce'); ?></a></li>
</ul>
回答by Tomanow
If you use a plugin like Theme My Loginyou can just create the link to the login page in your menu. It will display "Login" if the person is not logged in, and "Log Out" if the person is logged in. Hope this helps!
如果您使用像Theme My Login这样的插件,您只需在菜单中创建登录页面的链接即可。如果此人未登录,它将显示“登录”,如果此人已登录,它将显示“注销”。希望这有帮助!
回答by Luke
Logout Link: /?customer-logout=true
注销链接:/?customer-logout=true
Login link sent to My Account page to trigger login: /my-account/
登录链接发送到我的帐户页面以触发登录:/my-account/
回答by állan Fidelis Toledo
There are many codes that doesn't work. I just found one that works perfectly. Start functions.phpwith:
有很多代码不起作用。我刚刚找到了一个完美的作品。启动functions.php:
add_filter( 'wp_nav_menu_items', 'my_account_loginout_link', 10, 2 );
/**
* Add WooCommerce My Account Login/Logout to Menu
*
* @see https://support.woothemes.com/hc/en-us/articles/203106357-Add-Login-Logout-Links-To-The-Custom-Primary-Menu-Area
*/
function my_account_loginout_link( $items, $args ) {
if (is_user_logged_in() && $args->theme_location == 'top') { //change your theme location menu to suit
$items .= '<li><a class="nav-link" href="'. wp_logout_url( get_permalink( wc_get_page_id( 'shop' ) ) ) .'">Sair</a></li>'; //change logout link, here it goes to 'shop', you may want to put it to 'myaccount'
}
elseif (!is_user_logged_in() && $args->theme_location == 'top') {//change your theme location menu to suit
$items .= '<li><a class="nav-link" href="' . get_permalink( wc_get_page_id( 'myaccount' ) ) . '">Entrar</a></li>';
}
return $items;
}
回答by Denis Hue
Easier way is to change the menu structure
更简单的方法是更改菜单结构
Login (drop down menu)
登录(下拉菜单)
- Lost Password
- 忘记密码
My Account (drop down menu)
我的帐户(下拉菜单)
- Change Password
- Lost password
- Logout
- 更改密码
- 忘记密码
- 登出
then show or hide following page ID in woocommerce-functions.php (:123)
然后在 woocommerce-functions.php (:123) 中显示或隐藏以下页面 ID
function woocommerce_nav_menu_items( $items, $args ) {
if ( ! is_user_logged_in() ) {
$hide_pages = array();
$hide_pages[] = 20;
$hide_pages = apply_filters( 'woocommerce_logged_out_hidden_page_ids', $hide_pages );
foreach ( $items as $key => $item ) {
if ( ! empty( $item->object_id ) && ! empty( $item->object ) && in_array( $item->object_id, $hide_pages ) && $item->object == 'page' ) {
unset( $items[ $key ] );
}
}
} else {
$hide_pages = array();
$hide_pages[] = 18;
$hide_pages = apply_filters( 'woocommerce_logged_out_hidden_page_ids', $hide_pages );
foreach ( $items as $key => $item ) {
if ( ! empty( $item->object_id ) && ! empty( $item->object ) && in_array( $item->object_id, $hide_pages ) && $item->object == 'page' ) {
unset( $items[ $key ] );
}
}
}
return $items;
}
my login page id was '20', and my account page id was '18'
我的登录页面 ID 是“20”,我的帐户页面 ID 是“18”
Hope it helps anyone in need
希望能帮到有需要的人