php - 检查配置文件作者的角色并基于它应用图像
问题描述
我有这段代码检查用户角色并根据当前登录的用户浏览页面将图像添加到页面:
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;
}
谢谢。
解决方案
你需要改变一些事情:
您不需要同时使用
global $authordata
和get_the_author_meta
。你可以选择一个来使用。您尝试获取作者元的方式不正确。你只需要
user_level
像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()
;
短代码
对于可以在内容编辑器中调用的简码版本,使用与上述相同的函数 - 仅添加一个参数$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 /]
推荐阅读
- makefile - 从位于多个文件夹中的源文件生成目标文件
- http - 是否有一个 Golang Websocket 库可以让我修改初始请求/让我选择 http.Client?
- java - 运行时来自另一个 jar 的 Uber jar 的依赖项
- c# - 单击选项卡按钮时尝试加载文件
- php - 如何加入房间并将数据发送到php中的特定ID
- amazon-web-services - SAM 部署因 Lambda 函数的大小问题而失败
- python - 如何使用 {:10.4f}'.format( [Python] 动态更改值的精度
- python - 在 tkinter 中隐藏上一个按钮的输出
- android - 讲安卓工作室
- java - AWS EC2 实例不工作,但部署在 git 中说它应该在线