简体   繁体   中英

In wordpress How to display USER ROLE in comments

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 >👦 %s </font></cite>   <span class="user"> </span>' ), get_comment_author_link() );  ?>

[  ] 

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

User can have any role ie administrator, editor, author, subscriber or anything but should print user's role.

I want it to look like the picture, with the user role in square brackets.

在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM