10

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

2
  • This is the wrong approach. If there's something wrong with a plugin, try to fix that, instead of leaving it be and work on some other things. Since the plugin injects content via a filter function, you just need to remove that function.
    – tfrommen
    Commented Mar 10, 2014 at 18:57
  • @tf Thanks for your comment. Is it possible to remove the function just to get the correct the_content but keep it working? The thing is, I have a single post page that is OK to have the plugin's like buttons. What I'd like to do is to offer a full screen view of the same post. This is where I do not need the share buttons. Other than the full screen view, I need the plugin to show the buttons. Thanks Commented Mar 10, 2014 at 19:08

1 Answer 1

35

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/

2
  • thanks! It works. Sorry I cant vote up yet. Need more reputations Commented Mar 10, 2014 at 19:16
  • Thank you so much man . works like a charm.
    – Arman H
    Commented Apr 20, 2021 at 6:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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