php 停止 WordPress 自动显示 <p></p> 标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22710524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Stop WordPress automatically showing <p></p> tags
提问by batMask
Wordpress automatically generate lot of unwanted <p></p>
tags every where. Even the img
tag also wrap by these <p>
tags. So It's create unwanted white spaces in the site. I tried lot of ways to remove those tags:-
Wordpress 会<p></p>
在任何地方自动生成大量不需要的标签。甚至img
标签也被这些<p>
标签包裹。所以它会在网站中创建不需要的空白。我尝试了很多方法来删除这些标签:-
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', '', $content)
nothing works for me. First I want to know why those tags automatically generating? And How can I fix that?
没有什么对我有用。首先我想知道为什么这些标签会自动生成?我该如何解决?
回答by Rahil Wazir
Wordpress Visual Editor itself create those tags for new lines.
Wordpress 可视化编辑器本身为新行创建这些标签。
You can try to remove the filter wpautop
from the_excerpt
or the_content
您可以尝试wpautop
从the_excerpt
或删除过滤器the_content
remove_filter( 'the_content', 'wpautop' );
// OR
remove_filter( 'the_excerpt', 'wpautop' );
Sometimes it doesn't work (Didn't work for me in the past. Not with PHP).
有时它不起作用(过去对我不起作用。不适用于 PHP)。
You can try with Javascript/jQuery, place this code after DOM loaded or before the closing </body>
tag.
您可以尝试使用 Javascript/jQuery,将此代码放在 DOM 加载之后或结束</body>
标记之前。
jQuery('p:empty').remove();
Will remove all empty <p></p>
elements from all over the document.
<p></p>
将从整个文档中删除所有空元素。
回答by Ananth
you can try this plugin
你可以试试这个插件
http://wordpress.org/plugins/raw-html/
or else user this in theme's functions.php file
或者在主题的functions.php文件中使用它
remove_filter( 'the_content', 'wpautop' );
remove_filter( 'the_excerpt', 'wpautop' );
回答by Pieter Goosen
You need to run the remove_filter
function in the after_setup_theme
hook. The reason been is that it might get overrun by later on by the content or excerpt. So you would use the function as follow
您需要remove_filter
在after_setup_theme
钩子中运行该函数。原因是它可能会被内容或摘录所覆盖。所以你会使用这个函数如下
function remove_the_wpautop_function() {
remove_filter( 'the_content', 'wpautop' );
remove_filter( 'the_excerpt', 'wpautop' );
}
add_action( 'after_setup_theme', 'remove_the_wpautop_function' );
回答by ravi patel
Go into the theme editing area of WP admin.
进入WP admin的主题编辑区。
Add this code to your functions.php file, and save it.
将此代码添加到您的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);
?>
Now, when writing a post/page, use [raw][/raw] tags to surround that parts of the post/page that you do not want formatted.
现在,在编写帖子/页面时,使用 [raw][/raw] 标签将帖子/页面中您不想格式化的部分包围起来。
OR
或者
<?php the_content(); ?>
and place this directly above:
并将其直接放在上面:
<?php remove_filter ('the_content', 'wpautop'); ?>
it should look like this:
它应该是这样的:
<?php remove_filter ('the_content', 'wpautop'); ?>
<?php the_content(); ?>
回答by Bikram Shrestha
$content = preg_replace('#^<\/p>|<p>$#', '', $content);
echo do_shortcode($content);
I tried it while using it in shortcode and it worked for me.
我在短代码中使用它时尝试过它,它对我有用。
回答by Vishal Kumar Sahu
回答by Manesh Timilsina
There are lots of plugin to disable autop in WordPress or you can use following filter to remove autop:
有很多插件可以在 WordPress 中禁用 autop,或者您可以使用以下过滤器来删除 autop:
remove_filter( 'the_content', 'wpautop' );
However, if you need to style and format content from wp-editor, you will not be able to do so if you have removed autop.
但是,如果您需要为 wp-editor 中的内容设置样式和格式,如果您删除了 autop,您将无法这样做。
If you want to continue using autop functionality but want to remove empty p tag you can do it using jQuery.
如果您想继续使用 autop 功能但想删除空的 p 标签,您可以使用 jQuery 来完成。
I am able to remove unwanted empty p tags using following codes:
我可以使用以下代码删除不需要的空 p 标签:
jQuery(document).ready(function(){
$ = jQuery;
$('p').each(function() {
var $this = $(this);
if($this.html().replace(/\s| /g, '').length == 0)
$this.remove();
});
});