Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获得作者';在Wordpress中的角色_Wordpress - Fatal编程技术网

获得作者';在Wordpress中的角色

获得作者';在Wordpress中的角色,wordpress,Wordpress,我在我的第一个可湿性粉剂网站工作,需要显示一个作者的角色旁边的文章。类似于“Jimmy | Administrator”。查看可用的作者元数据:没有给我一种访问该元数据的方法。我相信有一个简单快捷的方法可以做到这一点,我只是不知道!!谢谢 更新:将其放在functions.php文件中: function get_author_role() { global $authordata; $author_roles = $authordata->roles; $aut

我在我的第一个可湿性粉剂网站工作,需要显示一个作者的角色旁边的文章。类似于“Jimmy | Administrator”。查看可用的作者元数据:没有给我一种访问该元数据的方法。我相信有一个简单快捷的方法可以做到这一点,我只是不知道!!谢谢

更新:将其放在functions.php文件中:

function get_author_role()
{
    global $authordata;

    $author_roles = $authordata->roles;
    $author_role = array_shift($author_roles);

    return $author_role;
}
然后在Wordpress循环中调用它。因此:

<?php
if(have_posts()) : while(have_posts()) : the_post();
    echo get_the_author().' | '.get_author_role();
endwhile;endif;
?>
如果你想抓住某篇文章的作者,就这样称呼它:

<?php
if(have_posts()) : while(have_posts()) : the_post();
    $aid = get_the_author_meta('ID');
    echo get_the_author().' | '.get_user_role($aid);
endwhile;endif;
?>

这将以“用户|角色”格式输出所需的信息。

它在循环之间工作正常,但如果我在存档页或作者页上添加,它会给我警告:
warning:array_shift()希望参数1为array,
中给出空值,您能帮我解决吗?@pixelngrain查看我的最新更新以获得您需要的答案。这应该允许您在循环外部获取所需的信息。请记住,您也可以在归档页面的循环内部使用post_author属性。
<?php
if(have_posts()) : while(have_posts()) : the_post();
    $aid = get_the_author_meta('ID');
    echo get_the_author().' | '.get_user_role($aid);
endwhile;endif;
?>
global $post;
$aid = $post->post_author;
echo get_the_author_meta('user_nicename', $aid).' | '.get_user_role($aid);