admin管理员组文章数量:1312852
Hey guys, I simply want to prevent the creation of empty paragraphs in my wordpress post. That happens quite often when trying to manually space content.
I don't know why this doesn't take effect?
/*Remove empty paragraph tags from the_content*/
function removeEmptyParagraphs($content) {
/*$pattern = "/<p[^>]*><\\/p[^>]*>/";
$content = preg_replace($pattern, '', $content);*/
$content = str_replace("<p></p>","",$content);
return $content;
}
add_filter('the_content', 'removeEmptyParagraphs');
edit/update:
seems like the problem is this:
function qanda($content) {
// filters for [q=some question] and [a=some answer]
// wraps it inside of <div class="qanda"><div class="question"> </div><div class="answer"> </div></div>
$content = preg_replace('/\[q=(.+?)].+?\[a=(.+?)]/is', '<div class="qanda"><div class="question">$1</div><div class="answer">$2</div></div>', $content);
return $content;
}
add_filter('the_content', 'qanda');
i did this function myself to filter for a kind of shortcode pattern in my posts and pages. Even though in my backend the post is completely done without paragraphs and unnecessary spacings the outcome looks like this:
<div class="entry">
<p></p>
<div class="qanda">...</div>
<p></p>
<p></p>
<div class="qanda">...</div>
<p></p>
<p></p>
<div class="qanda">...</div>
</div>
any idea where this empty p's come from?
Hey guys, I simply want to prevent the creation of empty paragraphs in my wordpress post. That happens quite often when trying to manually space content.
I don't know why this doesn't take effect?
/*Remove empty paragraph tags from the_content*/
function removeEmptyParagraphs($content) {
/*$pattern = "/<p[^>]*><\\/p[^>]*>/";
$content = preg_replace($pattern, '', $content);*/
$content = str_replace("<p></p>","",$content);
return $content;
}
add_filter('the_content', 'removeEmptyParagraphs');
edit/update:
seems like the problem is this:
function qanda($content) {
// filters for [q=some question] and [a=some answer]
// wraps it inside of <div class="qanda"><div class="question"> </div><div class="answer"> </div></div>
$content = preg_replace('/\[q=(.+?)].+?\[a=(.+?)]/is', '<div class="qanda"><div class="question">$1</div><div class="answer">$2</div></div>', $content);
return $content;
}
add_filter('the_content', 'qanda');
i did this function myself to filter for a kind of shortcode pattern in my posts and pages. Even though in my backend the post is completely done without paragraphs and unnecessary spacings the outcome looks like this:
<div class="entry">
<p></p>
<div class="qanda">...</div>
<p></p>
<p></p>
<div class="qanda">...</div>
<p></p>
<p></p>
<div class="qanda">...</div>
</div>
any idea where this empty p's come from?
Share Improve this question edited Apr 3, 2011 at 12:35 mathiregister asked Apr 3, 2011 at 10:13 mathiregistermathiregister 1,54313 gold badges55 silver badges78 bronze badges 3 |11 Answers
Reset to default 22WordPress will automatically insert <p>
and </p>
tags which separate content breaks within a post or page. If, for some reason, you want or need to remove these, you can use either of the following code snippets.
To completely disable the wpautop filter, you can use:
remove_filter('the_content', 'wpautop');
If you still want this to function try adding a later priority value to your filter something like:
add_filter('the_content', 'removeEmptyParagraphs',99999);
I had the same problem you have. I just did a... let's say... not very beautiful solution, but it works and so far it's the only solution I have. I added a little JavaScript line. It needs jQuery, but I'm sure you can figure it out without.
This is my tiny JS:
$('p:empty').remove();
This works for me!
Simply use CSS
p:empty {
display: none;
}
I know this is already marked 'solved' but just for reference, here's a function which does exactly what you want without having to add any markup to posts. Just put this in your theme's functions.php:
add_filter('the_content', 'remove_empty_p', 20, 1);
function remove_empty_p($content){
$content = force_balance_tags($content);
return preg_replace('#<p>\s*+(<br\s*/*>)?\s*</p>#i', '', $content);
}
This is from this gist: https://gist.github/1668216
You could just run your filter before that nasty wpautop
hooks on and messes with the markup.
add_filter('the_content', 'qanda', 7 );
That way, you've already converted what you need to by the time it hooks on, which does help in some cases.
Same approach than 2 answers before me, but an updated regex, because his didn't work for me.
the regex: /<p>(?:\s| )*?<\/p>/i
(non capture group looking for any number of either whitespace or
s inside p-tag, all case insenstive.
add_filter('the_content', function($content) {
$content = force_balance_tags($content);
return preg_replace('/<p>(?:\s| )*?<\/p>/i', '', $content);
}, 10, 1);
I found this weird, but actually calling the_content()
will insert paragraphs in the manner you describe. If you want the html code, basically like you entered it (the same as "view HTML" when editing), then use get_the_content()
which returns the content without formatting and paragraph tags.
Since it returns it, make sure you use something like:
echo get_the_content();
See also: http://codex.wordpress/Function_Reference/get_the_content
This will recursively remove all the empty html tags from the string
add_filter('the_content', 'remove_empty_tags_recursive', 20, 1);
function remove_empty_tags_recursive ($str, $repto = NULL) {
$str = force_balance_tags($str);
//** Return if string not given or empty.
if (!is_string ($str)
|| trim ($str) == '')
return $str;
//** Recursive empty HTML tags.
return preg_replace (
//** Pattern written by Junaid Atari.
'/<([^<\/>]*)>([\s]*?|(?R))<\/\1>/imsU',
//** Replace with nothing if string empty.
!is_string ($repto) ? '' : $repto,
//** Source string
$str
);}
Pattern is taken from http://codesnap.blogspot.in/2011/04/recursively-remove-empty-html-tags.html
If you have <p>
tags with whitespace in the content,
go to your post or page an edit it not in visual style.
you would be find some
in there..
Delete it and the empty <p>
tags will disappear.
In order to have only html content without
tags we can use the following loop to out put only the html without formatting of the post or page<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_title(); ?>
<?php echo $post->post_content; ?>
<?php endwhile; endif; ?>
If you are getting an empty <p>
tag at the top of your page or post, it is usually caused by wrapping the <?php the_content();?>
in paragraph tags.
PROBLEM
This is what causes the stray <p>
tag at the top of the page/post:
<p><?php the_content();?></p>
SOLUTION
If you remove the <p>
tags and only have the template tag, you'll solve the problem.
<?php the_content();?>
本文标签: functionsremove empty paragraphs from thecontent
版权声明:本文标题:functions - remove empty paragraphs from the_content? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741865605a2401882.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
wpautop
does it's thing, eg.add_filter('the_content', 'qanda', 7 );
.. – t31os Commented Apr 3, 2011 at 22:16