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

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

零分教程1,5532022-08-30 12:10

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 ) ;
END
零分站龄17年资深站长
一个喜欢折腾,却又折腾不出像样东西的,不会PHP的PHP程序员!
2517
文章
13
分类
3818
标签
3
友链
onlinelovesky317355746vipsever@vip.qq.com