首页 > 解决方案 > 检查配置文件作者的角色并基于它应用图像

问题描述

我有这段代码检查用户角色并根据当前登录的用户浏览页面将图像添加到页面:

 global $current_user; 
 get_currentuserinfo();
 switch (true)  {
  case ( user_can( $current_user, "talento_pro") ):
    echo '<i class="fa fa-bolt" title="Agência PRO+"></i>';
  break;
  case ( user_can( $current_user, "talento_pro_plus") ):
    echo '<i class="fa fa-rocket" title="Agência PRO+"></i>';
  break;
}

我怎样才能重现相同的内容,但它不是检查当前登录的用户,而是检查最初创建登录用户正在浏览的页面的用户的角色?

已编辑

我已经稍微调整了代码,但它不起作用,我认为它朝着正确的方向前进?

global $authordata; 
get_the_author_meta( "user_level" = "talento_pro" );

switch (true)  {
 case ( user_can( $authordata, "talento_pro") ):
   echo '<i title="Talento PRO" class="fa fa-bolt"></i>';
 break;
 case ( user_can( $author, "talento_pro_plus") ):
   echo '<i title="Talento PRO+" class="fa fa-rocket"></i>';
 break;
}

谢谢。

标签: phphtmlwordpress

解决方案


你需要改变一些事情:

  1. 您不需要同时使用global $authordataget_the_author_meta。你可以选择一个来使用。

  2. 您尝试获取作者元的方式不正确。你只需要user_levelget_the_author_meta( 'user_level' );

尝试这个:

    global $authordata;
    // This assumes that each user only has one role. You might have to adjust what array value you get
    $author_role = $authordata->roles[0];

        switch( $author_role ) {
            case 'talento_pro':
                echo '<i title="Talento PRO" class="fa fa-bolt"></i>';
                break;
            case 'talento_pro_plus':
                echo '<i title="Talento PRO+" class="fa fa-rocket"></i>';
                break;
        }

这会将role数组属性分配给$author_role变量,以便您可以在switch语句中检查它。

这是$authordata对象的样子:

WP_User Object
(
    [data] => stdClass Object
        (
            [ID] => 25
            [user_login] => Name
            [user_pass] => hashedpassword
            [user_nicename] => name
            [user_email] => name@example.com
            [user_url] => 
            [user_registered] => 2015-03-27 00:00:00
            [user_activation_key] => 
            [user_status] => 0
            [display_name] => Name
        )

    [ID] => 25
    [caps] => Array
        (
            [author] => 1
        )

    [cap_key] => wp_capabilities
    [roles] => Array
        (
            [0] => author
        )

    [allcaps] => Array
        (
            [upload_files] => 1
            [edit_posts] => 1
            [edit_published_posts] => 1
            [publish_posts] => 1
            [read] => 1
            [level_2] => 1
            [level_1] => 1
            [level_0] => 1
            [delete_posts] => 1
            [delete_published_posts] => 1
            [edit_attachments] => 1
            [delete_attachments] => 1
            [read_others_attachments] => 1
            [edit_others_attachments] => 1
            [delete_others_attachments] => 1
            [edit_aggregator-records] => 1
            [edit_published_aggregator-records] => 1
            [delete_aggregator-records] => 1
            [delete_published_aggregator-records] => 1
            [publish_aggregator-records] => 1
            [author] => 1
        )

    [filter] => 
    [site_id:WP_User:private] => 1
)

编辑:

如果要将其用作模板上的函数:

function wp03052019_get_user_role() {
    $user_role = '';
    global $authordata;
    // This assumes that each user only has one role. You might have to adjust what array value you get
    $author_role = $authordata->roles[0];

        switch( $author_role ) {
            case 'talento_pro':
                $user_role = '<i title="Talento PRO" class="fa fa-bolt"></i>';
                break;
            case 'talento_pro_plus':
                $user_role =  '<i title="Talento PRO+" class="fa fa-rocket"></i>';
                break;
        }
    return $user_role;
}

然后,您可以将其放入您的functions.php然后在您希望它输出的任何模板上调用该函数echo wp03052019_get_user_role()

短代码

对于可以在内容编辑器中调用的简码版本,使用与上述相同的函数 - 仅添加一个参数$atts- 因为它是必需的。

function wp03052019_get_user_role( $atts ) {
        $user_role = '';
        global $authordata;
        // This assumes that each user only has one role. You might have to adjust what array value you get
        $author_role = $authordata->roles[0];

            switch( $author_role ) {
                case 'talento_pro':
                    $user_role = '<i title="Talento PRO" class="fa fa-bolt"></i>';
                    break;
                case 'talento_pro_plus':
                    $user_role =  '<i title="Talento PRO+" class="fa fa-rocket"></i>';
                    break;
            }
        return $user_role;
    }

add_shortcode( 'userroleoutput', 'wp03052019_get_user_role'); 

现在,在您的内容编辑器中,您可以执行[userroleoutput /]


推荐阅读