【问题标题】:Wordpress keeps adding auto paragraph tags to shortcodes and contentWordpress 不断向简码​​和内容添加自动段落标签
【发布时间】:2019-06-18 04:42:18
【问题描述】:

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

【问题讨论】:

    标签: php html wordpress shortcode


    【解决方案1】:

    问题不在于 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;
    }
    

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

        我修改了@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 移动版的唠叨!

        【讨论】:

          猜你喜欢
          • 2011-07-10
          • 1970-01-01
          • 2015-09-12
          • 1970-01-01
          • 2017-05-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多