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

Partner 站长学院 2020-09-7 热度 1.35K℃ 收藏 本文共1712个字,预计阅读时间需要5分钟。

可以在文章的末尾自带添加一个作者信息框,进行一些简单的自我介绍

在WordPress文章的末尾,添加文章作者的相关信息,下面的代码就可以快速在文章中添加一个作者的信息框,一个好的作者信息框能够快速让用户记住该作者并关注!

在主题根目录下的functions.php文件中的<?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');

stylee.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在文章末尾自动添加一个作者信息框教程
  • 售后服务范围 1、商业模板使用范围内问题免费咨询
    2、源码安装、模板安装(一般 ¥50-300)服务答疑仅限会员用户
    3、单价超过200元的模板即可以免费一次安装,需提供服务器信息
    付费增值服务 1、提供dedecms模板、WordPress主题、discuz模板优化等服务请详询在线客服
    2、承接 WordPress、DedeCMS、Discuz 等系统建站、仿站、开发、定制等服务
    3、服务器环境配置安装(一般 ¥50-300)
    4、网站中毒处理(需额外付费,500元/次/质保三个月)
    售后服务时间 周一至周日(法定节假日除外) 9:00-23:00
    免责声明提示 本站所提供的资源仅供学习交流,若使用商业用途,请购买正版授权,否则产生的一切后果将由下载用户自行承担,有部分资源为网上收集或仿制而来,若模板侵犯了您的合法权益,请来信通知我们(Email: 495553705@qq.com),我们会及时删除,给您带来的不便,我们深表歉意!

低至 ¥99 元/年 爆款产品限时抢

阿里云*云小站 | 上云优惠聚集地 | 领取限量云产品优惠

立即抢购