In wordpress How to display USER ROLE in comments

2019-06-01 05:33发布

In my wordpress website, there is a comment template in which I made some changes which I want, but I am having an issue.

Note : I am using the User Role Editor plugin.

I want to display user role next to the username in each comment.

My Code :

    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();
}

I added this code into function.php but it didn't work.

Here is my comment-template.php section :

     <?php printf( __( ' <cite class="user" ><font size="5.5" color=red >                

标签: php wordpress
1条回答
Rolldiameter
2楼-- · 2019-06-01 06:17

You can use:

//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) ;
}

Hope this helps :-)

查看更多
登录 后发表回答