当前位置: 首页 » 教程 » WordPress获取文章作者the_author()无效问题解决正文

WordPress获取文章作者the_author()无效问题解决

  • 零分
  • 1,921

WordPress版本不同,遇到问题可能也不同,在文章页获取文章作者the_author()无效,原因是the_author()需要在循环中使用,即需要在

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
   the_author();
<?php endif; ?>

如在single页面上,不写循环,the_author();获取的将是空白,解决方法,用get_the_author_meta()来获取作者信息。

<?php echo get_the_author_meta( 'display_name', $post->post_author ) ?>

$post->post_author,得到文章作者ID,通过作者ID获取作者其他相关信息,get_the_author_meta()相关参数如下

user_login用户登录名
user_pass用户登录密码
user_nicename用户昵称
user_email用户邮箱地址
user_url用户网站地址
user_registered用户注册时间
user_status用户状态
display_name作者显示的名称
nickname作者昵称
first_name作者名字
last_name作者姓氏
description作者描述
user_level用户等级
user_firstname用户名字
user_lastname用户姓氏
user_description用户描述

调用方法:

get_the_author_meta("参数名",作者ID)
如:echo get_the_author_meta( 'display_name', $post->post_author ) ;

声明:博客中如无特殊说明或标注的文章均为博客原创文章,部分文章来源互联网,如有侵犯您的版权,或者对零分博客发布的文章有异议,请来信告知删除、修改,如您有好的建议或者意见也可以来信,谢谢!

请TA喝杯水^_^ 如本文“对您有用”,欢迎随意打赏,让我们坚持创作! 请TA喝水