最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - get author_name from queried post

programmeradmin0浏览0评论

All I'm trying to do is, get the author name of the current post and set it's name as a tag. I tried changing author_id to author_name but didn't really get anything back.

wp_set_post_tags should return 'user_nicename' (nickname) instead of 'hey'

All I'm trying to do is, get the author name of the current post and set it's name as a tag. I tried changing author_id to author_name but didn't really get anything back.

wp_set_post_tags should return 'user_nicename' (nickname) instead of 'hey'

Share Improve this question edited May 10, 2019 at 13:58 J patel asked May 9, 2019 at 19:50 J patelJ patel 154 bronze badges 1
  • 1 If you want the user_nicename value, then get_the_author_meta( 'user_nicename', $author_id ) would do it. And check the reference if you need help using the get_the_author_meta function. – Sally CJ Commented May 9, 2019 at 20:19
Add a comment  | 

1 Answer 1

Reset to default 0

The problem with your code lies in these three lines:

$queried_post = get_post($post_id);         
$author_id = $queried_post->post_author;
$first = $user_info->user_nicename;             

You correctly get authors ID, but then you try to get some field of user_info variable, which is not defined anywhere in your code.

One way to fix it is to get user info (using get_userdata) before trying to access it:

$author_id = $queried_post->post_author;
$user_info = get_userdata( $author_id );
$first = $user_info->user_nicename;  

You can also use get_the_author_meta to obtain only the field you need:

$author_id = $queried_post->post_author; 
$first = get_the_author_meta( 'user_nicename', $author_id );
发布评论

评论列表(0)

  1. 暂无评论