简体   繁体   English

WordPress:从post id获取作者信息

[英]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参数的有效值包括:

  • admin_color管理员颜色
  • aim目的
  • comment_shortcuts评论快捷方式
  • description描述
  • display_name显示名称
  • first_name
  • ID ID
  • jabber喋喋不休
  • last_name
  • nickname昵称
  • plugins_last_view plugins_last_view
  • plugins_per_page plugins_per_page
  • rich_editing rich_editing
  • syntax_highlighting语法高亮
  • user_activation_key用户激活密钥
  • user_description用户描述
  • user_email用户邮箱
  • user_firstname用户名
  • user_lastname用户姓氏
  • user_level用户级别
  • user_login用户登录
  • user_nicename用户名
  • user_pass用户通行证
  • user_registered用户注册
  • user_status用户状态
  • user_url用户网址
  • yim

This should work这应该工作

  • Post ID帖子编号
$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.

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