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

21次阅读
2024年09月13日 13:41:58

共计 1657 个字符,预计需要花费 5 分钟才能阅读完成。

如果想在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;
}
正文完
 0
鹿泽
版权声明:本站原创文章,本文由 鹿泽 于 2024年9月13日 13:41:58 发表,共计 1657 字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
本站所有文章,如无特殊说明或标注,均为本站原创发布。任何在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。
如若本站内容侵犯了原著者的合法权益,可联系我们邮箱:526009505@qq.com进行处理。
最新文章
网站优化seo推广服务如何做好内链?

网站优化seo推广服务如何做好内链?

在一个网站刚刚建设的初期,网站内链的建设工作尤为重要,可以说网站内链做得好SEO就成功了一半。今天就来讲讲网站...
WordPress博客主题二次元风-lolimeow主题更新10.1版,重构样式

WordPress博客主题二次元风-lolimeow主题更新10.1版,重构样式

一个WordPress博客主题二次元风主题,本站之前启用过,后面更换了,不过很好看,适合大家选择使用: 202...
专业优化公司seo是怎么做好404页面的?

专业优化公司seo是怎么做好404页面的?

404页面的目的是:告诉浏览者其所请求的页面不存在或链接错误,同时引导用户使用网站其它页面而不是关闭窗口离开。...
网站搜索优化seo如何做好内部链接优化?

网站搜索优化seo如何做好内部链接优化?

网站内部链接的布局很多做SEO的朋友都知道,非常重要。网站想做的越大,就要更加重视内部链接的布局。因为良好的内...
影响关键词优化根本的排序因素

影响关键词优化根本的排序因素

对于像百度搜索来说,并没有排序这一说法,搜索引擎认为排序是在特定的关键词下网站内容的位置,而关键词是由用户搜索...