[英]WordPress: get author info from post id
Or even the author id from the post id.甚至是帖子 ID 中的作者 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.
我正在使用自定义 function(见下文)返回帖子 ID,但我不确定接下来要调用什么 function。
function this_post_id() {
global $wp_query;
$thePostID = $wp_query->post->ID;
return $thePostID;
}
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 ); ?>
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;
?>
<?php
$field = 'display_name';
the_author_meta($field);
?>
Valid values for the $field
parameter include: $field
参数的有效值包括:
This should work这应该工作
$post_id = get_the_ID();
$author_id = get_post_field ('post_author', $post_id);
$display_name = get_the_author_meta( 'nickname' , $author_id );
echo $display_name;
Note: You need to use this function inside the loop.注意:您需要在循环中使用这个 function。
**use this code for display Auth Name**
<?php
$auth_id = $post->post_author;
echo get_the_author_meta( 'display_name', $auth_id );
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.