簡體   English   中英

Wordpress 一直在短代碼和內容中添加自動段落標簽

[英]Wordpress keeps adding auto paragraph tags to shortcodes and content

我似乎無法阻止 wordpress 自動將段落添加到我鍵入的每一行,包括短代碼和我輸入可視化編輯器的任何原始 HTML。 我已經嘗試使用插件“Toggle wpautop”和“Raw html”來嘗試轉換它,但是它從來沒有用過。 是因為我使用的是視覺作曲家嗎? 它只是將 p 標簽包裹在任何東西周圍。

在此處輸入圖像描述

問題不在於 Visual Composer,它的發生純粹是因為the_content上的autop過濾器。 有幾種方法可以解決它,但恕我直言,內容過濾器是處理它的最佳方法。

如果您願意編輯您的 functions.php,您可以過濾the_content鈎子,通過添加以下內容來刪除帶有strtr的短代碼周圍的<p>標簽:

add_filter('the_content', 'remove_unneeded_silly_p_tags_from_shortcodes');
function remove_unneeded_silly_p_tags_from_shortcodes($the_content){
    $array = array (
        '<p>['      => '[', //replace "<p>[" with "["
        ']</p>'     => ']', //replace "]</p>" with "]"
        ']<br />'   => ']' //replace "]<br />" with "]"
    );
    $the_content = strtr($the_content, $array); //replaces instances of the keys in the array with their values
    return $the_content;
}

其他替代方案(比如從 the_content 中刪除 autop )往往會產生相當深遠的影響,所以我傾向於避免這種情況。 您也可以嘗試從添加的特定段落標簽中刪除邊距樣式,但由於自動添加,可能很難定位該特定標簽...

試試這個,在你的函數中.php

remove_filter( 'the_content', 'wpautop' );
remove_filter( 'the_excerpt', 'wpautop' );

我修改了@Frits 的回答。 這往往會解決出現在屏幕底部的非常煩人的 Google Chrome 移動版“簡化視圖”問題。

add_filter('the_content', 'remove_unneeded_silly_p_tags_from_shortcodes');
function remove_unneeded_silly_p_tags_from_shortcodes($the_content){
    $array = array (
        '<p>'      => '',
        '</p>'     => '<br /><br />'
    );
    $the_content = strtr($the_content, $array); //replaces instances of the keys in the array with their values
    return $the_content;
}

一個問題是,它會刪除您添加到段落標簽的所有 CSS 樣式。 但好處很明顯:沒有 Google Chrome Mobile 的嘮叨!

我已經在這個問題上工作了幾個小時,經過多次嘗試,我發現了問題所在。

我的簡碼是從“Tinymce”自定義字段中獲取內容。 因此,為了正確顯示它,我在內容上使用apply_filters('the_content') 並且 Wordpress 將<p></p>添加到當前頁面中的所有后續簡碼,但不在此簡碼上(僅當它是第一個顯示時)。

答案是僅使用wpautop()來顯示此內容。

這可能是對某些人的暗示。
快樂的頭痛!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM