【问题标题】:Display Role name (ID) and Display Role Name显示角色名称 (ID) 和显示角色名称
【发布时间】:2021-04-06 08:17:12
【问题描述】:

以前有人问过这个问题,但没有给出答案。 我想显示 ROLE NAME 以及 ROLE ID

就角色 ID 而言,我使用以下方法成功显示:

if( is_user_logged_in() ) { 
     
     $user = wp_get_current_user(); 
     $roles =  $user->roles; 

     $_SESSION['roles']=$roles[0];

        return $roles; } 

但我想显示显示角色名称。 如下图,我可以打印senior_manager,但是不能打印Senior Manager

【问题讨论】:

标签: php arrays wordpress wordpress-theming user-roles


【解决方案1】:

我们只需将_ 替换为spacecapitalize。我们可以使用str_replace()ucwords()

<?php
/**
* is_user_logged_in()
* @link https://developer.wordpress.org/reference/functions/is_user_logged_in/
*/
if ( ! is_user_logged_in() ):

  /**
  * auth_redirect()
  * @link https://developer.wordpress.org/reference/functions/auth_redirect/
  */
  auth_redirect();
else:

  /**
  * wp_get_current_user()
  * @link https://developer.wordpress.org/reference/functions/wp_get_current_user/
  */
  if ( wp_get_current_user()->roles[0] !== '' ):
    echo ucwords( str_replace( '_', ' ', wp_get_current_user()->roles[0] ) );
  else:
    echo "Not available";
  endif;
endif; ?>

了解详情

【讨论】:

  • 谢谢。是的,这可以完成并且工作正常。但是,我只是在 WP 中寻找可以访问它的内置函数。
猜你喜欢
  • 2014-09-23
  • 2016-09-09
  • 2020-10-08
  • 2018-11-05
  • 2014-10-25
  • 2021-11-02
  • 2015-09-01
  • 2021-01-05
  • 1970-01-01
相关资源
最近更新 更多