繁体   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