Php 停止WordPress自动显示<;p></p>;标签

Php 停止WordPress自动显示<;p></p>;标签,php,html,css,wordpress,Php,Html,Css,Wordpress,Wordpress会在每个位置自动生成大量不需要的标签。即使是img标签也被这些标签包裹。因此,在站点中创建不需要的空白是非常必要的。我尝试了很多方法来移除这些标签:- preg_replace(array('<p>','</p>'),array('',''),the_content(),1); remove_filter( 'the_content', 'wpautop' ); preg_replace('/<p>\s*(<a .*>)?\s

Wordpress会在每个位置自动生成大量不需要的标签。即使是
img
标签也被这些
标签包裹。因此,在站点中创建不需要的空白是非常必要的。我尝试了很多方法来移除这些标签:-

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)
preg\u replace(数组(“”,“

”),数组(“,”),内容(),1); 移除_过滤器('the_content'、'wp autop'); preg_replace('/\s*()?\s*()\s*()?\s*/iU','\1\2\3',$content)

对我来说什么都不管用。首先我想知道为什么这些标签会自动生成?我怎样才能解决这个问题呢?

你可以试试这个插件

http://wordpress.org/plugins/raw-html/
或者在主题的functions.php文件中使用该用户

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

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

您可以尝试从
摘录
内容

remove_filter( 'the_content', 'wpautop' );
// OR
remove_filter( 'the_excerpt', 'wpautop' );
有时它不起作用(过去对我不起作用,PHP不行)

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

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

将从整个文档中删除所有空的

元素。

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

remove_filter( 'the_content', 'wpautop' );
但是,如果您需要从wp编辑器中设置内容的样式和格式,那么如果您删除了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();
}); 

});

您需要运行
after\u setup\u主题
hook中的
remove\u 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' );

进入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 remove_filter ('the_content', 'wpautop'); ?>

应该是这样的:

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

<?php the_content(); ?>

$content=preg_replace(“#^ |$#,”$content);
echo do_短代码($content);
我在短代码中使用它时尝试了它,它对我很有效。

我想它会很好-

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

在自动创建
的位置删除空格,也可以使用
jQuery('p:empty')。删除()
$content = preg_replace('#^<\/p>|<p>$#', '', $content);
echo do_shortcode($content);
function my_format_TinyMCE( $in ) {
  $in['wpautop'] = false;
  return $in;
}
add_filter( 'tiny_mce_before_init', 'my_format_TinyMCE' );