Вам нужно изменить несколько вещей:
Вам не нужны и 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()
;
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 /]