admin管理员组文章数量:1024416
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 Answer
Reset to default 0The 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 );
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, thenget_the_author_meta( 'user_nicename', $author_id )
would do it. And check the reference if you need help using theget_the_author_meta
function. – Sally CJ Commented May 9, 2019 at 20:19
1 Answer
Reset to default 0The 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 );
本文标签: phpget authorname from queried post
版权声明:本文标题:php - get author_name from queried post 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745506456a2153627.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
user_nicename
value, thenget_the_author_meta( 'user_nicename', $author_id )
would do it. And check the reference if you need help using theget_the_author_meta
function. – Sally CJ Commented May 9, 2019 at 20:19