溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

在WordPress 文章未尾自動添加一個作者信息框的方法

發(fā)布時間:2020-08-17 16:05:33 來源:億速云 閱讀:158 作者:小新 欄目:建站服務(wù)器

這篇文章主要介紹在WordPress 文章未尾自動添加一個作者信息框的方法,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!

如果想在WordPress文章的末尾,添加文章作者的相關(guān)信息,下面一段代碼可以方便在文章中添加一個作者的信息框。

將代碼添加到當(dāng)前主題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 );
        // 獲取作者的網(wǎng)站
        $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">關(guān)于 ' . $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>';
        // 檢查作者在個人資料中是否填寫了網(wǎng)站
        if ( ! empty( $user_website ) ) {
        // 顯示作者的網(wǎng)站鏈接
        $author_details .= ' | <a href="' . $user_website .'" target="_blank" rel="nofollow">網(wǎng)站</a></div>';
        } else {
            // 如果作者沒有填寫網(wǎng)站則不顯示網(wǎng)站鏈接
            $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 文章未尾自動添加一個作者信息框的方法的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI