php WordPress:从帖子 ID 中获取作者信息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3188901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:57:13  来源:igfitidea点击:

WordPress: get author info from post id

phpwordpress

提问by superUntitled

Or even the author id from the post id. I am trying to return the author meta (author page link and avatar) in the sidebar of a single post page (outside of the post loop). What is the best way to do this? I am using a custom function (see below) to return the post id, but am not sure what function to call next.

甚至是来自帖子 ID 的作者 ID。我试图在单个帖子页面的侧边栏中(帖子循环之外)返回作者元数据(作者页面链接和头像)。做这个的最好方式是什么?我正在使用自定义函数(见下文)返回帖子 ID,但不确定接下来要调用什么函数。

function this_post_id() {
  global $wp_query;
  $thePostID = $wp_query->post->ID;
  return $thePostID;
}

回答by superUntitled

I figured it out.

我想到了。

<?php $author_id=$post->post_author; ?>
<img src="<?php the_author_meta( 'avatar' , $author_id ); ?> " width="140" height="140" class="avatar" alt="<?php echo the_author_meta( 'display_name' , $author_id ); ?>" />
<?php the_author_meta( 'user_nicename' , $author_id ); ?> 

回答by ssourav

If you want it outside of loop then use the below code.

如果您想在循环之外使用它,请使用以下代码。

<?php
$author_id = get_post_field ('post_author', $cause_id);
$display_name = get_the_author_meta( 'display_name' , $author_id ); 
echo $display_name;
?>