造成這個問題的原因是 WordPress 默認的日誌內容處理流程中,wpautop(將回車轉換成 p 或者 br 標籤的函數)是在 Shortcode 前面運行的。所以我們的解決方案也是非常簡單,改變它們執行的順序,在當前主題的 functions.php
文件中添加:
remove_filter( 'the_content', 'wpautop' ); add_filter( 'the_content', 'wpautop' , 12);
這樣調整順序之後,你的 shortcode 裡面的內容,就不會有自動添加的 p 或者 br 標籤,但是如果 shortcode 中部分的內容你又需要一些 p 或者 br 標籤用來換行的話,你需要自己手動在自己 shortcode 處理程序中添加 wpautop 來處理了。
function bio_shortcode($atts, $content = null) { $content = wpautop(trim($content)); return '<div class="bio">' . $content . '</div>'; } add_shortcode('bio', 'bio_shortcode');
Shortcode 詳細介紹和更多技巧:
本站長期承接 WordPress 項目,詳情請加 QQ:11497107 或者 Email:denis@wpjam.com。
如有 WordPress 問題,請到 WPJAM 問答提問!QQ 上不方便解答!
想在手機閱讀更多Wordpress資訊?下載【香港矽谷】Android應用 