php - 在wordpress中如何在评论中显示用户角色

标签 php wordpress

在我的 WordPress 网站中,有一个评论模板,我在其中做了一些我想要的更改,但我遇到了问题。

注意:我正在使用 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 中,但它不起作用。

这是我的 comment-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

最佳答案

您可以使用:

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

关于php - 在wordpress中如何在评论中显示用户角色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34037576/

相关文章:

php - 两个项目共享相同的用户帐户

php包含文件执行失败

php - 获取在 MySQL 中找到数据的列名

php - 时间戳更新问题

javascript - JS 和可能的 Wordpress 中未捕获的语法错误

php - 垂直对齐 Wordpress 页脚中自定义创建的菜单

php - 使用 PHP 检测 Web 浏览器语言

css - wordpress子主题两次加载子样式,没有父样式

php - WordPress $wpdb->get_results 即使为空也会返回日期

mysql - 无法远程连接到在 EC2 实例(非 RDS)上运行的 MySQL 实例