[英]Stop WordPress automatically showing <p></p> tags
Wordpress automatically generate lot of unwanted <p></p>
tags every where. WordPress会在每个地方自动生成很多不需要的<p></p>
标签。 Even the img
tag also wrap by these <p>
tags. 甚至img
标签也被这些<p>
标签包裹。 So It's create unwanted white spaces in the site. 因此,它在站点中创建了多余的空白。 I tried lot of ways to remove those tags:- 我尝试了多种方法来删除这些标签:-
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)
nothing works for me. 没有什么对我有用。 First I want to know why those tags automatically generating? 首先,我想知道为什么这些标记会自动生成? And How can I fix that? 我该如何解决?
Wordpress Visual Editor itself create those tags for new lines. Wordpress Visual Editor本身会为新行创建这些标签。
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>
元素。
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' );
You need to run the remove_filter
function in the after_setup_theme
hook. 您需要在after_setup_theme
挂钩中运行remove_filter
函数。 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 所以您将使用以下功能
function remove_the_wpautop_function() {
remove_filter( 'the_content', 'wpautop' );
remove_filter( 'the_excerpt', 'wpautop' );
}
add_action( 'after_setup_theme', 'remove_the_wpautop_function' );
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();
});
});
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(); ?>
$content = preg_replace('#^<\/p>|<p>$#', '', $content);
echo do_shortcode($content);
I tried it while using it in shortcode and it worked for me. 我在短代码中使用它时尝试过,它对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.