2013-03-02 166 views
1

我想弄清楚如何添加登錄/註銷菜單。當我將這段代碼添加到wordpress頭部時,內容和側欄消失。我如何將登錄/註銷添加到菜單而不會丟失我的頁面的其餘部分。我曾嘗試在設置菜單中添加它,但它不適用於我正在使用的主題。添加登錄/註銷菜單Woocommerce 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> 

回答

4

如果您使用插件像Theme My Login你可以創建你的菜單鏈接到登錄頁面。如果該用戶未登錄,它將顯示「登錄」,如果該用戶登錄,則顯示「登出」。希望這有助於您!

0

更簡單的方法是改變菜單結構

登錄(下拉菜單)

  • 忘記密碼:

我的賬戶(下拉菜單)

  • 變化密碼
  • 丟失密碼
  • 註銷

然後顯示或woocommerce-functions.php的隱藏下頁ID(123)

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; 
} 

我的登錄頁面ID是 '20',和我的賬戶頁面ID是'18'

希望它可以幫助有需要的人

2

註銷鏈接: /客戶註銷=真正

登錄鏈接發送到我的帳戶頁面觸發登錄: /my-account/

+0

我喜歡這種簡單的答案:) – DeFeNdog 2015-02-06 17:57:12

1

有很多代碼不起作用。我只找到一個完美的作品。開始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; 
}