欢迎您光临自学哈网,只为分享网络知识教程,供大家学习参考!

「自学哈网」在WordPress 文章未尾自动添加一个作者信息框

作者 : 自学哈 本文共1655个字,预计阅读时间需要5分钟 2022-11-6 共165人阅读
也想出现在这里? 联系我们

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

如果想在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' );// 允许HTMLremove_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;}
本站声明:
本站所有资源来源于网络,分享目的仅供大家学习和交流!如若本站内容侵犯了原著者的合法权益,可联系邮箱976157886@qq.com进行删除。
自学哈专注于免费提供最新的分享知识、网络教程、网络技术的资源分享平台,好资源不私藏,大家一起分享!

自学哈网 » 「自学哈网」在WordPress 文章未尾自动添加一个作者信息框
也想出现在这里? 联系我们
© 2022 Theme by - 自学哈网 & WordPress Theme. All rights reserved 浙ICP备2022016594号