dongpo5264 2017-04-25 15:38
浏览 33
已采纳

如何让wp_logout_url与wp_nav_menu一起工作?

I'm new to wp and now trying to create a link for logging out like this:

      if ( is_user_logged_in() ) { 
        wp_nav_menu( array( 'theme_location'  => 'header-menu', 'depth' => 0,'sort_column' => 'menu_order', 'items_wrap' => '<ul id="%1$s" class="nav navbar-nav">%3$s<li><a href="<?php echo esc_url(wp_logout_url( home_url())) ?>">Logout</a></li></ul>' ) );
      }

The other parts of code are from the theme. I only want to add logout but the result shows that it's not found. Anybody knows how to make it work?

  • 写回答

1条回答 默认 最新

  • dongsui0929 2017-04-25 15:56
    关注

    You can achieve this using the wp_nav_menu_items hook. Let's have a look at the following piece of code which shows the login/logout link on the header-menu location.

    add_filter( 'wp_nav_menu_items', 'pd_logout_menu_link', 10, 2 );
    
    function pd_logout_menu_link( $menu_items, $args ) {
       if ($args->theme_location == 'header-menu') {
          if (is_user_logged_in()) {
             $menu_items .= '<li><a href="'. wp_logout_url() .'">'. __("Log Out") .'</a></li>';
          }
       }
       return $menu_items;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码