[英]WordPress: get author info from post id
甚至是帖子 ID 中的作者 ID。 我试图在单个帖子页面的侧边栏(在帖子循环之外)返回作者元数据(作者页面链接和头像)。 做这个的最好方式是什么? 我正在使用自定义 function(见下文)返回帖子 ID,但我不确定接下来要调用什么 function。
function this_post_id() {
global $wp_query;
$thePostID = $wp_query->post->ID;
return $thePostID;
}
我想到了。
<?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 ); ?>
如果您想在循环之外使用它,请使用以下代码。
<?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);
?>
$field
参数的有效值包括:
这应该工作
$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;
注意:您需要在循环中使用这个 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.