从the_content删除空的段落?
-
-
看到问题:[我正在使用过滤器来删除
标签自动换行](http://wordpress.stackexchange.com/questions/7846)
See the question: [I'm using a filter to remove thetags auto wrap](http://wordpress.stackexchange.com/questions/7846)
- 0
- 2011-04-03
- Chris_O
-
尝试在运行" wpautop"之前运行过滤器,例如.`add_filter('the_content','qanda',7); ..Try running your filter before `wpautop` does it's thing, eg. `add_filter('the_content', 'qanda', 7 );`..
- 1
- 2011-04-03
- t31os
-
@t31os:您可以将评论移至答案,以便我们对其进行投票吗?@t31os: Can you move your comment to an answer so we can vote on it?
- 0
- 2011-04-06
- Jan Fabry
-
10 个回答
- 投票数
-
- 2011-04-03
WordPress将自动插入
<p>
和</p>
标记,这些标记将内容分隔在帖子或页面中.如果出于某种原因想要或需要删除它们,则可以使用以下任一代码段.要完全禁用wpautop过滤器,可以使用:
remove_filter('the_content', 'wpautop');
如果您仍然希望此功能起作用,请尝试在过滤器中添加一个更高的优先级值,例如:
add_filter('the_content', 'removeEmptyParagraphs',99999);
WordPress 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);
-
谢谢!我知道wordpress会自动插入p标签.但是,有时在我的内容中某个地方(当我使用某种工具进行检查时)只有空的
和自动格式化.我就是不想有空的p!
thank you! I know wordpress automatically inserts p tags. However there happen some cases where there are just empty tags somewhere in my content (when i inspect it with some tool)... that happens when doing a lot of removal and editing of posts. I just don't want to have empty paragraphs in my content, that's all. I do need paragraphs, just not empty ones. The 99999 doesn't make a difference. Just doesn't work. the wpautop filter is not what I want. It prevents all's and autoformatting. I just don't want any empty p's!
- 2
- 2011-04-03
- mathiregister
-
我更新了我的帖子,所以您明白我的意思了!我做了一个已经过滤内容的功能.它插入div,似乎wordpress在它之前和之后插入i updated my post so you see what I mean! i did a function that already filters the content. it inserts divs and it seemes wordpress is inserting before and after it, i just don't get it. any ideas?
- 0
- 2011-04-03
- mathiregister
-
- 2012-01-02
我遇到了同样的问题.我只是做了一个...可以说...不是很漂亮的解决方案,但是它有效,到目前为止,这是我唯一的解决方案.我添加了一些JavaScript行.它需要jQuery,但我敢肯定,没有它就可以弄清楚.
这是我的小JS:
$('p:empty').remove();
这对我有用!
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!
-
哦,那不是一个甜蜜的小数字!感谢您的提示-它对我有用,如果其他人想知道如何使用它,只需将其放在主题的自定义JS文件中即可.oh aint that a sweet little number! Thanks for the tip - it works for me and in case anyone else wondered how to use it, just put it in your theme's custom JS file.
- 0
- 2012-09-06
- Sol
-
@D_N使用CSS隐藏空的段落标签仅适用于
\n
.@D_N Using CSS to hide empty Paragraph tags only works for `` but doesn't work for `\n
`.- 0
- 2017-04-21
- Michael Ecklund
-
- 2015-09-30
只需使用CSS
p:empty { display: none; }
Simply use CSS
p:empty { display: none; }
-
请在回答中添加解释Please add an explanation to your answer
- 0
- 2015-09-30
- Pieter Goosen
-
@PieterGoosen已经不言自明了@PieterGoosen it is already self-explanatory
- 4
- 2015-10-01
- at least three characters
-
如果您只是想避免出于间距目的而显示它们,则此版本在IE9之前效果都很好.http://caniuse.com/#feat=css-sel3和https://developer.mozilla.org/en-US/docs/Web/CSS/%3Aempty了解更多.If you just want to avoid displaying them for spacing purposes, this works well down to IE9. http://caniuse.com/#feat=css-sel3 and https://developer.mozilla.org/en-US/docs/Web/CSS/%3Aempty for more.
- 0
- 2016-01-03
- Will
-
CSS选择器方法的不错选择,不知道它是否存在.谢谢!nice option with CSS selector method, didn't know it existed. thanks!
- 1
- 2016-04-14
- i_a
-
仅供参考:如果
标记内有`&nbsp;`,将无法正常工作.
FYI: If there is ` ` inside thetag this won't work.
- 1
- 2019-06-06
- RynoRn
-
- 2012-05-22
我知道这已经被标记为"已解决",但是仅供参考,这是一个功能,可以完全满足您的需要,而无需在帖子中添加任何标记.只需将其放入主题的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); }
这来自以下要点: https://gist.github.com/1668216
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.com/1668216
-
关于使用force_balance_tags()的一点说明... 在包含JavaScript的内容上使用此函数时,我遇到了一个棘手的错误(当在表单上使用ajax时,JS来自Gravity Forms).在某些情况下,如果遇到" <"字符,"force_balance_tags"会出现已知问题.有关详细信息,请参见票证[9270](http://core.trac.wordpress.org/ticket/9270).Just a little note about using force_balance_tags()... I ran into a tricky bug caused by this function when it was used on content that included JavaScript (JS was coming from Gravity Forms when using ajax on a form). There are known problems with `force_balance_tags` when it encounters the `<` character in certain situations. See ticket [9270]( http://core.trac.wordpress.org/ticket/9270) for details.
- 6
- 2013-08-14
- Dave Romsey
-
我遇到了Dave强调的相同问题:该代码段删除了嵌入的youtube视频,这也导致放大器页面上的验证问题.I had the same problem highlighted by Dave: the snippet removed embedded youtube video and that caused validation problems on amp pages also.
- 0
- 2019-06-29
- Marco Panichi
-
- 2011-04-06
您可以在该讨厌的
wpautop
挂接并弄乱标记之前运行过滤器.add_filter('the_content', 'qanda', 7 );
这样,您已经在挂钩时将所需的内容转换了,在某些情况下确实有帮助.
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.
-
- 2017-12-06
与我面前的2个答案相同的方法, 但是更新了正则表达式,因为他对我不起作用.
正则表达式:
/<p>(?:\s| )*?<\/p>/i
(非捕获组在p标签内查找任意数量的空格或
,所有情况均与情况无关.add_filter('the_content', function($content) { $content = force_balance_tags($content); return preg_replace('/<p>(?:\s| )*?<\/p>/i', '', $content); }, 10, 1);
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);
-
- 2011-04-03
我发现这很奇怪,但是实际上调用
the_content()
将以您描述的方式插入段落.如果您想要html代码,基本上就像您输入的一样(与编辑时的"查看HTML"相同),则可以使用get_the_content()
返回不带格式和段落标签的内容.因为它返回了它,所以请确保使用类似以下内容的东西:
回声get_the_content();
另请参见: http://codex.wordpress.org/Function_Reference/get_the_content >
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 useget_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.org/Function_Reference/get_the_content
-
好,谢谢.但是我不想要那个!我需要正常的段落.首先是语义标记,其次就是它应该采用的方式.我只是没有没有意义的空段!仅仅是因为我对这些段落应用了样式,所以这种样式也会出现空的段落,并且我的页面看起来很奇怪.well, thank you. However I don't want that! I need normal paragraphs. First of it's semantic markup and secondly it's just the way it's supposed to. I just don't to have empty paragraphs that don't make sense! Simply because I have styling applied to those paragraphs also empty paragraphs appear with this styling and my page looks weird.
- 0
- 2011-04-03
- mathiregister
-
我实际上想知道为什么我的add_filter东西无法正常工作?I actuall wonder why my add_filter thingy does not work?
- 0
- 2011-04-03
- mathiregister
-
知道了好吧,我建议您尝试的一件事是从HTML切换到视觉,再返回一两个时间.我相信在所见即所得编辑器加载时,它确实会删除空的段落标签.Gotcha. Well one thing I would recommend trying is switching from HTML to visual and back a time or two. I believe when the WYSIWYG editor loads it does remove empty paragraph tags.
- 0
- 2011-04-04
- cwd
-
- 2014-05-07
这将递归删除字符串中的所有空html标记
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 );}
模式取自 http://codesnap.blogspot.in/2011/04/recursively-remove-empty-html-tags.html
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
-
- 2017-05-15
如果您的
<p>
标记的内容中带有空格, 转到您的帖子或页面,以非视觉风格对其进行编辑.您会在其中找到一些
. 删除它,空的<p>
标签将消失.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. -
- 2018-09-19
为了只包含不带
<?php if (have_posts()) : while (have_posts()) : the_post(); ?> <?php the_title(); ?> <?php echo $post->post_content; ?> <?php endwhile; endif; ?>
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; ?>
嗨, 我只是想防止在wordpress帖子中创建空的段落.尝试手动间隔内容时,这种情况经常发生.
我不知道为什么这没有生效?
编辑/更新:
似乎是这样的问题:
我自己执行了此功能,以过滤帖子和页面中的一种短码模式.即使在我的后端,帖子完全没有段落和多余的空格,结果看起来还是这样:
知道这个空p的来源吗?