這篇文章主要介紹在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 文章未尾自動添加一個作者信息框的方法的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。