如何在WordPress文章末尾自动添加作者简介

  • 时间:2020-05-29 14:26:07
  • 分类:网络文摘
  • 阅读:82 次

对于多用户的 wordpress 网站,由于每篇贴子的作者各不相同,那么在文章末尾添加一个关于作者的信息框不仅有助于读者增加对作者本人的了解,还可以促进作者与读者之间的互动,提升其写作动力。

如果你的的 wordpress主题没有此功能,可以使用 wordpress插件 Author Bio Box 来添加作者简介。但插件会占用系统资源,同时若插件更新不及时,容易在wordpress更新后与其发生冲突,造成网站运行故障。所以这不是本文的目的,本文介绍的方法是通过代码自动在每一篇文章的末尾添加一个作者信息框。

一、在后台编辑相关用户资料,如添加站点地址、个人说明(支持html语言)等信息。

wordpress添加个人说明

二、添加如下代码到当前主题的 functions.php 文件中。

  1. function wp_author_info_box( $content ) {
  2.     global $post;
  3.     // 检测文章与文章作者
  4.     if ( is_single() && isset( $post->post_author ) ) {
  5.         // 获取作者名称
  6.         $display_name = get_the_author_meta( 'display_name', $post->post_author );
  7.         // 如果没有名称,使用昵称
  8.         if ( emptyempty( $display_name ) )
  9.         $display_name = get_the_author_meta( 'nickname', $post->post_author );
  10.         // 作者的个人信息
  11.         $user_description = get_the_author_meta( 'user_description', $post->post_author );
  12.         // 获取作者的网站
  13.         $user_website = get_the_author_meta('url', $post->post_author);
  14.         // 作者存档页面链接
  15.         $user_posts = get_author_posts_url( get_the_author_meta( 'ID' , $post->post_author));
  16.         if ( ! emptyempty( $display_name ) )
  17.         $author_details = '<div class="author-name">关于 ' . $display_name . '</div>';
  18.         if ( ! emptyempty( $user_description ) )
  19.         // 作者头像
  20.         $author_details .= '<div class="author-details">' . get_avatar( get_the_author_meta('user_email') , 90 ) . nl2br( $user_description ). '</div>';
  21.         $author_details .= '<div class="author-links"><a href="http://uuxn.com/add-author-info-in-wordpress-posts/'. $user_posts .'">查看 ' . $display_name . ' 所有文章</a>';
  22.         // 检查作者在个人资料中是否填写了网站
  23.         if ( ! emptyempty( $user_website ) ) {
  24.         // 显示作者的网站链接
  25.         $author_details .= ' | <a href="http://uuxn.com/add-author-info-in-wordpress-posts/' . $user_website .'" target="_blank" rel="nofollow">网站</a></div>';
  26.         } else {
  27.             // 如果作者没有填写网站则不显示网站链接
  28.             $author_details .= '</div>';
  29.         }
  30.         // 在文章后面添加作者信息
  31.         $content = $content . '<footer class="author-bio-section" >' . $author_details . '</footer>';
  32.     }
  33.     return $content;
  34. }
  35. // 添加过滤器
  36. add_action( 'the_content', 'wp_author_info_box' );
  37. // 允许HTML
  38. remove_filter('pre_user_description', 'wp_filter_kses');

三、再将配套的CSS代码添加到主题样式文件 style.css 中。

  1. .author-bio-section {
  2.     background: #fff;
  3.     float: left;
  4.     width: 100%;
  5.     margin: 10px 0;
  6.     padding: 15px;
  7.     border: 1px dashed #ccc;
  8. }
  9. .author-name {
  10.     font-size: 15px;
  11.     font-weight: bold;
  12.     margin: 0 0 5px 0;
  13. }
  14. .author-details img {
  15.     float: left;
  16.     width: 48px;
  17.     height: auto;
  18.     margin: 5px 15px 0 0;
  19. }

最终显示效果如下图所示:

添加wordpress作者简介演示图

注:如果修改 style.css 文件后没有效果,请参阅:wordpress主题更新style.css样式表文件后不生效的解决方法。

推荐阅读:
调查称槟榔是一级致癌物可引发口腔癌  嚼食槟榔对身体健康的危害非常大  槟榔被认定为一级致癌物可引发口腔癌  食品安全监管工作的有效性令人疑惑  厂家称没法根本解决五芳斋粽子发霉  饮食保健:盘点枸杞的十大养生功效  食物为何会致癌及常见致癌物来源  肯德基麦当劳可食用冰块菌落超标  南瓜的保健作用:降血糖血脂、防癌  大暑养生:暑天应多吃清淡易消化食物 
评论列表
添加评论