当前位置:首页 » 好文分享 » 详情

    在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' );
    // 允许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;
    }

    照教程添加就可以了。
    打赏
    X
    打赏方式:
    • 支付宝
    • 微信
    • QQ红包

    打开支付宝扫一扫
    日期:2017年10月12日 09:39:59 星期四   分类:好文分享   浏览(34043)
    本文地址:https://www.blogs.hk/post-2695.html   [百度已收录]
    声明:本页信息由网友自行发布或来源于网络,真实性、合法性由发布人负责,请仔细甄别!本站只为传递信息,我们不做任何双方证明,也不承担任何法律责任。文章内容若侵犯你的权益,请联系本站删除!
    版权所有:《博客之家
    文章标题:《在WordPress 文章未尾自动添加一个作者信息框
    除非注明,文章均为 《博客之家》 原创
    转载请注明本文短网址:https://www.blogs.hk/post-2695.html  [生成短网址]

    留言咨询

    自动获取QQ

    昵称

    邮箱

    网址

        站点统计

        • 收录网址:3370 个
        • 发布文章:3008 条
        • 在线人数:1人
        • 总访问量:270549515次
        • 本站运行:12年5月3天
        Copyright © 2025 博客之家 版权所有  
        關於本站免責聲明sitemap新站登錄