繁体   English   中英

WordPress:从post id获取作者信息

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

  • 管理员颜色
  • 目的
  • 评论快捷方式
  • 描述
  • 显示名称
  • ID
  • 喋喋不休
  • 昵称
  • plugins_last_view
  • plugins_per_page
  • rich_editing
  • 语法高亮
  • 用户激活密钥
  • 用户描述
  • 用户邮箱
  • 用户名
  • 用户姓氏
  • 用户级别
  • 用户登录
  • 用户名
  • 用户通行证
  • 用户注册
  • 用户状态
  • 用户网址

这应该工作

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

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