如何在WordPress 文章未尾自動添加一個作者信息框

下面由WordPress教程欄目給大家介紹在wordpress 文章未尾自動添加一個作者信息框的方法,希望對需要的朋友有所幫助!

如何在WordPress 文章未尾自動添加一個作者信息框

如果想在wordpress教程文章的末尾,添加文章作者的相關信息,下面一段代碼可以方便在文章中添加一個作者的信息框。

將代碼添加到當前主題functions.php中:

function?wp_author_info_box(?$content?)?{ ????global?$post; ????//?檢測文章與文章作者 ????if?(?is_single()?&amp;&amp;?isset(?$post-&gt;post_author?)?)?{ ????????//?獲取作者名稱 ????????$display_name?=?get_the_author_meta(?'display_name',?$post-&gt;post_author?); ????????//?如果沒有名稱,使用昵稱 ????????if?(?empty(?$display_name?)?) ????????$display_name?=?get_the_author_meta(?'nickname',?$post-&gt;post_author?); ????????//?作者的個人信息 ????????$user_description?=?get_the_author_meta(?'user_description',?$post-&gt;post_author?); ????????//?獲取作者的網站 ????????$user_website?=?get_the_author_meta('url',?$post-&gt;post_author); ????????//?作者存檔頁面鏈接 ????????$user_posts?=?get_author_posts_url(?get_the_author_meta(?'ID'?,?$post-&gt;post_author)); ????????if?(?!?empty(?$display_name?)?) ????????$author_details?=?'<div>關于?'?.?$display_name?.?'</div>'; ????????if?(?!?empty(?$user_description?)?) ????????//?作者頭像 ????????$author_details?.=?'<div>'?.?get_avatar(?get_the_author_meta('user_email')?,?90?)?.?nl2br(?$user_description?).?'</div>'; ????????$author_details?.=?'<div> <a>查看?'?.?$display_name?.?'?所有文章</a>'; ????????//?檢查作者在個人資料中是否填寫了網站 ????????if?(?!?empty(?$user_website?)?)?{ ????????//?顯示作者的網站鏈接 ????????$author_details?.=?'?|?<a>網站</a> </div>'; ????????}?else?{ ????????????//?如果作者沒有填寫網站則不顯示網站鏈接 ????????????$author_details?.=?''; ????????} ????????//?在文章后面添加作者信息 ????????$content?=?$content?.?'<footer>'?.?$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; }

? 版權聲明
THE END
喜歡就支持一下吧
點贊15 分享