【问题标题】:Stop WordPress automatically showing <p></p> tags停止 WordPress 自动显示 <p></p> 标签
【发布时间】:2014-05-07 18:41:20
【问题描述】:

Wordpress 会自动生成大量不需要的&lt;p&gt;&lt;/p&gt; 标签。甚至img 标签也被这些&lt;p&gt; 标签包裹。所以它在站点中创建了不需要的空白。我尝试了很多方法来删除这些标签:-

preg_replace(array('<p>','</p>'),array('',''),the_content(),1);

remove_filter( 'the_content', 'wpautop' );

preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content)

没有什么对我有用。首先我想知道为什么这些标签会自动生成?我该如何解决这个问题?

【问题讨论】:

标签: php html css wordpress


【解决方案1】:

Wordpress 可视化编辑器本身为新行创建这些标签。

您可以尝试从the_excerptthe_content 中删除过滤器wpautop

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

有时它不起作用(过去对我不起作用。不适用于 PHP)。

您可以尝试使用 Javascript/jQuery,将此代码放在 DOM 加载之后或关闭 &lt;/body&gt; 标记之前。

jQuery('p:empty').remove();

将从整个文档中删除所有空的 &lt;p&gt;&lt;/p&gt; 元素。

【讨论】:

    【解决方案2】:

    你可以试试这个插件

    http://wordpress.org/plugins/raw-html/
    

    或者在主题的functions.php文件中使用这个

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

    【讨论】:

      【解决方案3】:

      您需要在after_setup_theme 挂钩中运行remove_filter 函数。原因是它稍后可能会被内容或摘录所淹没。所以你会使用如下函数

      function remove_the_wpautop_function() {
          remove_filter( 'the_content', 'wpautop' );
          remove_filter( 'the_excerpt', 'wpautop' );
      }
      
      add_action( 'after_setup_theme', 'remove_the_wpautop_function' );
      

      【讨论】:

        【解决方案4】:

        进入 WP admin 的主题编辑区。

        将此代码添加到您的functions.php 文件中,然后保存。

        <?php
        function my_formatter($content) {
            $new_content = '';
            $pattern_full = '{(\[raw\].*?\[/raw\])}is';
            $pattern_contents = '{\[raw\](.*?)\[/raw\]}is';
            $pieces = preg_split($pattern_full, $content, -1, PREG_SPLIT_DELIM_CAPTURE);
        
            foreach ($pieces as $piece) {
                if (preg_match($pattern_contents, $piece, $matches)) {
                    $new_content .= $matches[1];
                } else {
                    $new_content .= wptexturize(wpautop($piece));
                }
            }
        
            return $new_content;
        }
        
        remove_filter('the_content', 'wpautop');
        remove_filter('the_content', 'wptexturize');
        
        add_filter('the_content', 'my_formatter', 99);
        ?>
        

        现在,在撰写文章/页面时,使用 [raw][/raw] 标签将其包围 您不想格式化的帖子/页面部分。

         <?php the_content(); ?>
        

        并将其直接放在上面:

        <?php remove_filter ('the_content', 'wpautop'); ?>
        

        应该是这样的:

        <?php remove_filter ('the_content', 'wpautop'); ?>
        
        <?php the_content(); ?>
        

        【讨论】:

          【解决方案5】:

          WordPress 中有很多插件可以禁用 autop,或者您可以使用以下过滤器来删除 autop:

          remove_filter( 'the_content', 'wpautop' );
          

          但是,如果您需要对 wp-editor 中的内容进行样式设置和格式化,如果您删除了 autop,您将无法这样做。

          如果您想继续使用 autop 功能但又想删除空的 p 标签,您可以使用 jQuery 来完成。

          我可以使用以下代码删除不需要的空 p 标签:

          jQuery(document).ready(function(){
              $ = jQuery; 
              $('p').each(function() {
              var $this = $(this);
              if($this.html().replace(/\s|&nbsp;/g, '').length == 0)
                  $this.remove();
          }); 
          
          });
          

          【讨论】:

            【解决方案6】:
            $content = preg_replace('#^<\/p>|<p>$#', '', $content);
            echo do_shortcode($content);
            

            我在短代码中使用它时尝试过,它对我有用。

            【讨论】:

              【解决方案7】:

              我猜this 可以正常工作-

              function my_format_TinyMCE( $in ) {
                $in['wpautop'] = false;
                return $in;
              }
              add_filter( 'tiny_mce_before_init', 'my_format_TinyMCE' );
              

              【讨论】:

                猜你喜欢
                • 2012-06-30
                • 1970-01-01
                • 1970-01-01
                • 2021-07-23
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多