如何在WordPress中使用PHP获取文章作者的用户名?
WordPress 获取文章作者用户名的正确方法
你遇到的问题是用错了get_the_author_meta的参数——WordPress里存储用户登录名(即你要的用户名)的字段是user_login,而非username,这就是修改后代码失效的原因。
正确的代码写法如下:
$post_id = get_the_ID(); $author_id = get_post_field('post_author', $post_id); $username = get_the_author_meta('user_login', $author_id); // 输出用户名 echo $username;
如果你的代码是在文章循环内部,还可以进一步简化,不用单独获取文章ID和作者ID,直接调用即可:
// 文章循环内直接获取当前作者用户名 $username = get_the_author_meta('user_login'); echo $username;
内容的提问来源于stack exchange,提问作者Red
相关产品推荐
相关产品推荐

