2015-12-02 67 views
1

在我的wordpress网站中,有一个评论模板,我在其中进行了一些我想要的更改,但是我遇到了问题。在wordpress中如何在评论中显示USER ROLE

注意:我正在使用User Role Editor插件。

我想在每条评论中显示用户角色旁边的用户名

我的代码:

function get_the_author_role() { 
    global $wpdb, $wp_roles, $authordata; 

    if (!isset($wp_roles)) 
     $wp_roles = new WP_Roles(); 

    foreach($wp_roles->role_names as $role => $Role) { 
     $caps = $wpdb->prefix . 'capabilities'; 
     if (array_key_exists($role, $authordata->$caps)) 
      return $Role; 
    } 
} 

/** 
* Echo the account role of the author of the current post in the Loop. 
* @see get_the_author_role() 
* @return null 
*/ 
function the_author_role() { 
    echo get_the_author_role(); 
} 

我将此代码添加到function.php,但没有奏效。

这里是我的评论-的template.php部分:

 <?php printf(__(' <cite class="user" ><font size="5.5" color=red > %s </font></cite> <span class="user"> </span>'), get_comment_author_link()); ?> 

[ ] 

      <?php if ('0' == $comment->comment_approved) : ?> 

用户可以有任意角色即管理员,编辑,作者,用户或什么,但应打印用户的角色。

我希望它看起来像图片,方括号中的用户角色。

enter image description here

+0

你使用角色管理的任何插件? –

+0

是\t 用户角色编辑器 –

回答

5

您可以使用:

//get the commented user id 
$user_id = get_comment(get_comment_ID())->user_id; 

if ($user_id) 
{ 
    $user_info = get_userdata($user_id); 
    echo implode(', ', $user_info->roles) ; 
} 

希望这有助于:-)

+0

是的,这很好,非常感谢你 –

+0

如果它帮助你 – Sabari