dongyun8891 2019-04-29 16:00
浏览 263

在WordPress中添加双向菜单以进行登录和注销

I am looking to add tow menus in wordpress theme using hooks. One menu should show on when members logged in my system and second menu will show when members are logout from system.

I am using below code but it's showing only for logged out users.

function custom_new_menu() {
  register_nav_menus(array('custom-menu' => __( 'Custom Menu' ),'extra-menu' => __( 'Extra Menu')));
}
add_action( 'init', 'custom_new_menu' );

  • 写回答

1条回答 默认 最新

  • doupapin172773 2019-04-29 17:12
    关注

    You can utilize the is_user_logged_in() function to check if the current user is logged into Wordpress.

    function custom_new_menu() {
      if (is_user_logged_in()) {
        register_nav_menu(
          // Loads the additional nav for logged in users
          'extra-menu'  => __( 'Extra Menu')
        );
      } else {
        register_nav_menu(
          // Loads the default nav for visitors
          'custom-menu' => __( 'Custom Menu' ),
        );
      }
    } 
    
    add_action( 'init', 'custom_new_menu' );
    
    • Note that the code above switched from the register_nav_menus() which registers multiple menus to register_nav_menu() which registers a single menu. The syntax for these is slightly different as we no longer need the inner array.
    评论

报告相同问题?

悬赏问题

  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 arduino控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题