WordPress 技巧:如何解決 Shortcode 中自動添加的 br 或者 p 標籤

.. 我們在使用 WordPress Shortcode API 開發插件的時候,有個比較麻煩的問題,就是 WordPress 會自動在 shortcode 內添加 br 或者 p 標籤,這樣可能會打亂你的原先預想的 HTML 結構和布局。

造成這個問題的原因是 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應用
分享到Facebook
技術平台: Nasthon Systems