9

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

  • 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 Mar 10 '14 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 – Charles Wayne Mar 10 '14 at 19:08
26

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:

(boolean or integer) (optional) Preserve line breaks. When set to true, any line breaks remaining after paragraph conversion are converted to HTML <br />. Line breaks within script and style sections are not affected.

Source: https://codex.wordpress.org/Function_Reference/wpautop

  • thanks! It works. Sorry I cant vote up yet. Need more reputations – Charles Wayne Mar 10 '14 at 19:16

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.