WordPress中有太多自定义的内容,并且没有统一的标准。 WordPress的版本也很多,版本之间的调用方法也不同。 您在网上找到的教程可能是旧版本,而新版本可能用不了…
例如,wordpress文章页面获取作者的标签。网上的教程都是“<?php the_author(); ?> ”,但是此标记只能在循环中使用。 您不能在single.php中使用它,其输出的是空白。
所以用什么标签呢?最后才查到用“get_the_author_meta()”这个函数来实现。使用方法如下:
1
|
<?php echo get_the_author_meta( 'display_name', $post->post_author ) ?>
|
函数有以下几种调用参数:
- 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(用户描述)
结合以上就可以根据自己需求调用啦。
如果我们的WordPress博客有多个作者发表文章时候,我们会需要在文章内显示作者的昵称、头像等资料。
在WordPress实现作者头像的方法也很简单,一行函数代码即可调用到。
调用作者头像
WordPress调用当前文章作者头像
<?php echo get_avatar( get_the_author_email(), '60' );?>
WordPress 在meta标签中添加文章作者名字
在为 WordPress 主题添加meta property
标签时,其中有一项是:文章源自知更鸟-https://zmingcx.com/article-author-name-in-wordpress-meta-tag.html
- <meta property="og:author" content="作者" />
有个问题,网上常见的 WordPress 代码是:文章源自知更鸟-https://zmingcx.com/article-author-name-in-wordpress-meta-tag.html
- <meta property="og:author" content="<?php the_author(); ?>" />
其中:文章源自知更鸟-https://zmingcx.com/article-author-name-in-wordpress-meta-tag.html
- <?php the_author(); ?>
在这里使用不能显示作者名称,不知道帖代码者是怎么显示作者名称的,可能都是转抄,不知所以然。文章源自知更鸟-https://zmingcx.com/article-author-name-in-wordpress-meta-tag.html
解决办法,用:文章源自知更鸟-https://zmingcx.com/article-author-name-in-wordpress-meta-tag.html
- <?php the_author_meta('user_nicename', $post->post_author); ?>
替换。文章源自知更鸟-https://zmingcx.com/article-author-name-in-wordpress-meta-tag.html
其中默认user_nicename 是显示作者的登录名称,暴露重要信息有些危险。可以用下面的替换一下:文章源自知更鸟-https://zmingcx.com/article-author-name-in-wordpress-meta-tag.html
- display_name 公开显示名称
- user_nicename 昵称
- first_name 名字
- last_name 姓氏
建议用:display_name,这是“公开显示”设置中的名称。文章源自知更鸟-https://zmingcx.com/article-author-name-in-wordpress-meta-tag.html
至于完整的meta property
标签写法,因主题不同,获取信息的方法也不同,这里就不写了,网上搜一堆,如果不显示作者名称按上面改一下。