检查个人资料作者的角色并根据其应用图片

时间:2019-03-05 22:36:58

标签: php html wordpress

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

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

谢谢。

1 个答案:

答案 0 :(得分:1)

您需要更改一些内容:

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

  2. 您尝试获取作者元数据的方式不正确。您只需要像user_level

  3. 那样传递get_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()的任何模板上调用该函数;

SHORTCODE

对于可以在内容编辑器中调用的简码版本,请使用与上述相同的功能-仅添加单个参数$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 /]