简体   繁体   English

PHP代码显示WP作者并且未登录用户

[英]PHP code to display WP author and not logged in user

I've been modifying a long abandoned plugin project that provides a Stackoverflow inspired badge system for Wordpress. 我一直在修改一个长期废弃的插件项目,该项目为Wordpress提供了Stackoverflow启发式徽章系统。 I have upgraded its code so it functions with the new Wordpress version. 我已经升级了其代码,因此它可以与新的Wordpress版本一起使用。

You can view the entire code here if interested: http://pastebin.com/kCWWLPL2 如果有兴趣,可以在这里查看完整的代码: http : //pastebin.com/kCWWLPL2

I want to add a code to the author.php page to list the author's awarded badges. 我想向author.php页面添加代码以列出作者的获奖徽章。 So far, I have only managed to display the logged in user's badges with this working code: 到目前为止,我仅使用以下工作代码来显示已登录用户的徽章:

<?php
$user_id = get_current_user_id();
if ($user_id != 0) {
  rhb_list_badges(array('user_ID' => $user_id));
}
?>

I was told to use this code to display the author's badges: 有人告诉我使用此代码来显示作者的徽章:

<?php
$author = get_user_by( 'slug', get_query_var( 'author_name' ) ); 
if ($author->ID > 0) {
  rhb_list_badges(array('user_ID' => $author->ID));
}
?>

But it does not return anything on the page. 但是它不会在页面上返回任何内容。 Why? 为什么? What am I doing wrong here? 我在这里做错了什么? How can I alter the working get_current_user_id code example so that it will display the author's badges and not the logged in user's one ? 如何更改有效的get_current_user_id代码示例,以使其显示作者的徽章,而不显示登录用户的徽章

You could use this before the Loop on the author.php page. 您可以在author.php页面上的循环之前使用它。

<?php
$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
rhb_list_badges(array('user_ID'=>$curauth->ID));
?>

Taken from the WordPress Codex . 取自WordPress Codex

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM