Вопрос

Wordpress is creating a &nbsp in the_excerpt. How do I remove it?

<div class="subtitulo-noticia"><?php the_excerpt(); ?></div>

enter image description here

&nbsp creates a space at the beginning of p

enter image description here

Edit: Each space in the_content creates a &nbsp, this ends up creating a &nbsp in the_excerpt. How do I create spaces in content without creating &nbsp

Это было полезно?

Решение

I'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>&nbsp; ', '<p>&nbsp;' ], '<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( "&nbsp","",get_the_excerpt() );

Maybe, replacing &nbsp with an empty string could be fix your problem. The get_the_excerpt() function will get your post's excerpt and str_replace() substitute &nbsp.

This is my working solution:

<?php echo trim(preg_replace('/>\s+</m', '><', get_the_content()));?>
Лицензировано под: CC-BY-SA с атрибуция
Не связан с wordpress.stackexchange
scroll top