admin管理员组文章数量:1125424
Wordpress is creating a   in the_excerpt. How do I remove it?
<div class="subtitulo-noticia"><?php the_excerpt(); ?></div>
  creates a space at the beginning of p
Edit: Each space in the_content creates a  , this ends up creating a   in the_excerpt. How do I create spaces in content without creating  
Wordpress is creating a   in the_excerpt. How do I remove it?
<div class="subtitulo-noticia"><?php the_excerpt(); ?></div>
  creates a space at the beginning of p
Edit: Each space in the_content creates a  , this ends up creating a   in the_excerpt. How do I create spaces in content without creating  
Share Improve this question edited Jan 30, 2017 at 22:39 Gabriel Souza asked Jan 30, 2017 at 15:21 Gabriel SouzaGabriel Souza 3711 gold badge10 silver badges23 bronze badges 5 |6 Answers
Reset to default 1I'm not sure why there's a non-breaking space being added to the front of your excerpt. It would be best to find out what is putting that there. But in the meantime, you can add this to your theme functions.php ( or a simple plugin ) and it should take care of it.
//* Remove non-breaking space from beginning of paragraph
add_filter( 'the_excerpt', function( $excerpt ) {
return str_replace( [ '<p> ', '<p> ' ], '<p>', $excerpt );
}, 999, 1 );
If you can't get rid of the extra space in the admin, then use trim()
<div class="subtitulo-noticia"><?php echo trim(get_the_excerpt()); ?></div>
http://www.w3schools.com/php/func_string_trim.asp
Try with:
$text = str_replace( " ","",get_the_excerpt() );
Maybe, replacing  
with an empty string could be fix your problem.
The get_the_excerpt()
function will get your post's excerpt and str_replace()
substitute  
.
This is my working solution:
<?php echo trim(preg_replace('/>\s+</m', '><', get_the_content()));?>
I used a modified version of Nathan's solution to resolve the issue of WordPress adding in extra empty heading and space tags. While not comprehensive, it does save me from editing 200+ pages in the CMS to bring them up to accessibility level compliance. Also, we know that even if we fix the 200+ pages someone will do it again, so it would become a reoccurring issue to resolve.
// Remove non-breaking space from beginning of paragraph
add_filter( 'the_content', function ( $content ) {
$search = [
'<h1></h1>',
'<h2></h2>',
'<h3></h3>',
'<h4></h4>',
'<h5></h5>',
'<h6></h6>',
'<p></p>',
'<span></span>',
' ',
];
return str_replace( $search, '', $content );
}, 999 );
Insert this code into your theme's functions.php file
add_filter( 'the_excerpt', function( $excerpt ) {
return str_replace( ' ', '', $excerpt );
}, 999, 1);
本文标签: phpRemove ampnbsp from theexcerpt
版权声明:本文标题:php - Remove &nbsp from the_excerpt 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736657609a1946295.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
delete
key – RobBenz Commented Jan 30, 2017 at 19:38