溫馨提示×

溫馨提示×

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

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

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

發布時間:2020-08-17 16:05:33 來源:億速云 閱讀:204 作者:小新 欄目:建站服務器

這篇文章主要介紹在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;
}

以上是在WordPress 文章未尾自動添加一個作者信息框的方法的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女