dtrn74832
2016-07-27 08:13
浏览 97
已采纳

如何替换我的帐户并在Woocommerce中添加登录/注销链接到菜单?

I am using woocommerce and I add the one menu My Account using woocommerce plugin But I want to show the menu login and logout instead of My Account which I add the into menu. I also added the script into functions.php result is same.

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_navigation') {

       //echo "hello friend how are";

       $items .= '<li><a href="'. wp_logout_url( get_permalink( woocommerce_get_page_id( 'myaccount' ) ) ) .'">Log Out</a></li>';

   }

   elseif (!is_user_logged_in() && $args->theme_location == 'primary_navigation') {

       $items .= '<li><a href="' . get_permalink( woocommerce_get_page_id( 'myaccount' ) ) . '">Log In</a></li>';

   }

   return $items;

}

When I am using jupitor theme and when I see the theme location of My Account Menu I get

Primary Navigation I have a one doubt is I have to add the My Account menu first then I add the login and logout menu.

图片转代码服务由CSDN问答提供 功能建议

我正在使用woocommerce,我使用woocommerce插件添加一个菜单My Account但是我想显示菜单登录和 注销而不是我添加到菜单中的我的帐户。 我还将脚本添加到functions.php结果相同。

  add_filter('wp_nav_menu_items','add_loginout_link',10,2); 
 
function add_loginout_link($ items  ,$ args){
 
 if(is_user_logged_in()&amp;&amp; $ args-&gt; theme_location =='primary_navigation'){
 
 // echo“hello friend how are”; 
 
 $  items。='&lt; li&gt;&lt; a href =“'。wp_logout_url(get_permalink(woocommerce_get_page_id('myaccount')))。'”&gt;退出&lt; / a&gt;&lt; / li&gt;'; 
 
  } 
 
 elseif(!is_user_logged_in()&amp;&amp; $ args-&gt; theme_location =='primary_navigation'){
 
 $ items。='&lt; li&gt;&lt; a href =“'。get_permalink  (woocommerce_get_page_id('myaccount'))。'“&gt;登录&lt; / a&gt;&lt; / li&gt;'; 
 
} 
 
返回$ items; 
 
} 
    
 
 

当我使用jupitor主题时,当我看到“我的帐户菜单”的主题位置时,我得到

主要导航 我有一个疑问是我必须首先添加“我的帐户”菜单然后添加日志 in和logout菜单。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongmi1221 2016-07-27 10:15
    已采纳

    Your above code will add login/logout links to menu. You do not have to add My Account menu.

    Please check your theme_location. If theme location is correct then login/logout link will add to menu.

    For check theme_location you have to search register_nav_menus in functions.php file of your theme. If you find it in functions.php file then you can see theme_location inside register_nav_menus code.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题