可以在文章的末尾自帶添加一個作者信息框,進行一些簡單的自我介紹
訪客們都是來也匆匆,去也匆匆,為了給匆匆的訪客們留下一個良好且深刻的印象,可以在文章的末尾自帶添加一個作者信息框,進行一些簡單的自我介紹。
- 原文來源:詳情

代碼部署
在主題根目錄下的functions.php
文件中的<?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');
樣式美化
在主題根目錄下的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;
}