post_content with line breaks

I want to be able to get post_content with line breaks. What is the best way to do it?

I previously use the_content() which was working fine until I needed to install a plugin that adds share buttons on each post.

Since then the output of my the_content includes texts from the share buttons.

I tried to use post_contents which contains the correct content I need to output on my template file but the problem is the line breaks were stripped off.

Is it possible to get post_content with line breaks preserved?

Thanks

1
1

I believe this should work:

$getPost = get_the_content();
$postwithbreaks = wpautop( $getPost, true/false );
echo $postwithbreaks;

The second argument in wpautop can be up to you whether it’s true of false, see the link below. It is described as follows:

(bool) (Optional) If set, this will convert all remaining line breaks after paragraphing. Line breaks within <script>, <style>, and <svg> tags are not affected.

Default value: true

Source:
https://developer.wordpress.org/reference/functions/wpautop/

Leave a Comment