博客之家
  • 博客首页
  • 博客導航
  • 好文分享
  • 文章投稿
  • 给我留言
首页 未分类 正文

在WordPress 文章未尾自动添加一个作者信息框

博客之家 2017-10-12 09:39 未分类 阅读 3.57w

如果想在wordpress文章的未尾,添加文章作者的相关信息,下面一段代码可以帮助方便在文章中添加一个作者的信息框。



将代码添加到当前主题functions.php中:

function wp_author_info_box( $content ) {
    global $post;
    // 检测文章与文章作者
    if ( is_single() && isset( $post->post_author ) ) {
        // 获取作者名称
        $display_name = get_the_author_meta( 'display_name', $post->post_author );
        // 如果没有名称,使用昵称
        if ( empty( $display_name ) )
        $display_name = get_the_author_meta( 'nickname', $post->post_author );
        // 作者的个人信息
        $user_description = get_the_author_meta( 'user_description', $post->post_author );
        // 获取作者的网站
        $user_website = get_the_author_meta('url', $post->post_author);
        // 作者存档页面链接
        $user_posts = get_author_posts_url( get_the_author_meta( 'ID' , $post->post_author));
        if ( ! empty( $display_name ) )
        $author_details = '<div class="author-name">关于 ' . $display_name . '</div>';
        if ( ! empty( $user_description ) )
        // 作者头像
        $author_details .= '<div class="author-details">' . get_avatar( get_the_author_meta('user_email') , 90 ) . nl2br( $user_description ). '</div>';
        $author_details .= '<div class="author-links"><a href="'. $user_posts .'">查看 ' . $display_name . ' 所有文章</a>';
        // 检查作者在个人资料中是否填写了网站
        if ( ! empty( $user_website ) ) {
        // 显示作者的网站链接
        $author_details .= ' | <a href="' . $user_website .'" target="_blank" rel="nofollow">网站</a></div>';
        } else {
            // 如果作者没有填写网站则不显示网站链接
            $author_details .= '</div>';
        }
        // 在文章后面添加作者信息
        $content = $content . '<footer class="author-bio-section" >' . $author_details . '</footer>';
    }
    return $content;
}
// 添加过滤器
add_action( 'the_content', 'wp_author_info_box' );
// 允许HTML
remove_filter('pre_user_description', 'wp_filter_kses');
再将配套的CSS添加到主题样式文件style.css中:
.author-bio-section {
    background: #fff;
    float: left;
    width: 100%;
    margin: 10px 0;
    padding: 15px;
    border: 1px dashed #ccc;
}
.author-name {
    font-size: 15px;
    font-weight: bold;
    margin: 0 0 5px 0;
}
.author-details img {
    float: left;
    width: 48px;
    height: auto;
    margin: 5px 15px 0 0;
}



照教程添加就可以了。

文章作者:博客之家
文章標題:在WordPress 文章未尾自动添加一个作者信息框
文章鏈接:https://www.likinming.com/2695.html
本站所有文章除特別聲明外,均採用 CC BY-NC-SA 4.0 許可協議,轉載請註明來自博客之家 !

📚 推荐阅读

  • 拒绝这些行为,不然百度会判定你是作弊!
  • WordPress添加鼠标点击页面显示文字JS特效
  • wordpress百度sitemap导致的curl_setopt警告错误解决办法
  • 网站文章更新正确的方式
  • 我是姜辰,不是任何人,这就是我
  • 网站标签的属性分析
  • 老幼咸宜的养生汤水
  • 做好这两步,怎么样都会有点排名!

懂得付出的人最幸福
« 上一篇 2017-10-13
人生路上,我们需要学会承受
下一篇 » 2017-10-12

发表评论

请输入验证码
还没有评论, 告诉我们你的想法

搜尋有價值的

热门文章

  • TOP1
    博客之家今天正式上線
    14年前 202.38w
  • TOP2
    博客之家导航源码分享
    13年前 102.62w
  • TOP3
    李明的鲜果店
    9年前 87.76w
  • TOP4
    iPhone6 ios8不显示通话记录处理方法
    12年前 60.42w
  • TOP5
    给我们投稿
    14年前 56.85w
  • TOP6
    超级SEO外链工具源码分享
    14年前 52.06w
  • TOP7
    超级SEO外链工具
    14年前 40.03w
  • TOP8
    我的人生我作主——李明
    13年前 39.83w
  • TOP9
    腾讯电脑管家误报网站的申诉方法
    9年前 34.42w
  • TOP10
    博客之家改版成功
    14年前 29.41w
  • TOP11
    利用:before选择器给你的网站Logo添加扫光特效
    11年前 27.87w
  • TOP12
    多做运动的好处
    13年前 24.61w

最新评论

  • 博客之家5 个月前
    藏福守心,遠離是非,日子平穩,歲月安康
  • 张赛博客6 个月前
    有时候,错误的路上,努力越多,反而是沉没...
  • atmp1 年前
    心安即是归处
  • 博客之家1 年前
    @Eps工具网:Hi,午安...
  • Eps工具网1 年前
    早晨起来
  • Eps工具网1 年前
    来逛逛
  • Eps工具网1 年前
    Eps工具网
  • 今日头条新闻2021-07-01 17:33
    文章不错交个朋友
  • 栾。2021-05-04 22:59
    看看
  • 夏草2021-02-22 10:27
    远古外星人 (2009-2019)史前...

友情連結

  • A TECH ONE
  • EPS工具網
  • 2W工具網
  • 小杰工具箱
  • 舍力博客
  • 提交收錄
Copyright © 2026 博客之家 版權所有
關於本站| 免責聲明| sitemap| 簡體| 繁體
内容受版权保护,禁止复制!