I need to delete the whitespace that the_excerpt(); function returns me. I noticed it gives me whitespace before the whole text only where in the page images goes first and only after goes text.

I tried str_replace(); and trim(); none of them helped.

The image of how it looks


2 Answers 2


This is caused by the line break between the first image in the post and the body content of the post. Just delete the one line of space between the two and it will working fine.


This filter removes all possible whitespace at the beginning of the excerpt, including  .

add_filter( 'get_the_excerpt', 'wpse162725_ltrim_excerpt' );

function wpse162725_ltrim_excerpt( $excerpt ) {
    return preg_replace( '~^(\s*(?: )?)*~i', '', $excerpt );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.