html WordPress不断添加自动段落标记到短代码和内容

v1uwarro  于 2022-11-27  发布在  WordPress
关注(0)|答案(4)|浏览(139)

我似乎无法阻止wordpress在我输入的每一行中自动添加段落,包括我输入到visual composer中的短代码和任何原始HTML。我试过插件“Toggle wpautop”和“Raw html”来转换它,但是它从来没有工作过。是因为我使用visual composer吗?它只是在任何东西周围包裹p标签。

xytpbqjk

xytpbqjk1#

这个问题不是Visual Composer的问题,它的发生纯粹是因为the_content上的autop过滤器。有几种方法可以解决它,但IMHO内容过滤器是最好的解决方法。
如果你能熟练地编辑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;
}

其他的替代方法(like removing autop from the_content)往往会产生非常深远的影响,所以我倾向于避免这种情况。您也可以尝试从添加的特定段落标记中删除边距样式,但由于自动添加,可能很难针对特定标记...

pes8fvy9

pes8fvy92#

试试这个,在你的functions.php中

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

l7mqbcuq3#

我修改了@Frits的回答。这有助于修复非常烦人的谷歌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样式。但好处是显而易见的:没有谷歌浏览器移动的唠叨!

uidvcgyl

uidvcgyl4#

我已经在这个问题上工作了几个小时,经过多次尝试,我发现了问题。
我的简码是从一个“Tinymce”自定义域中获取内容。所以为了正确显示它,我在内容上使用了apply_filters('the_content')。WordPress在当前页面的所有后续简码中添加了<p></p>,但不在此简码上(只有当它是第一个显示的时候)。
答案是只使用wpautop()来显示此内容。
这对某些人来说可能是个暗示。
快乐的头痛!!

相关问题