WordPress Get Content With Formatting
Hi Friends, There are lots of way to get content in WordPress and sometimes it happens that when you print content it is not showing properly,means it shows content without formatting. It is really very frustrating when you didn’t do anything but your content not shows up properly. Sometimes it will take lots of time to solve this types of error, So I thought I should post its solution here So it will save time of someone and help to solve it.
Anyway let me explain what was problem, I was having page id and from that page id I was fetching data by get_post($id, $output, $filter) function. But when I was printing content of post it then all lines were showing continuously without breaking to other lines. I checked my content twice and in admin content was showing properly with breaks but don’t know why it was not working on my front side where I want to show it properly.
I tried lots of other ways but every time same issue. Now let me tell you what I did. First I used below code :
$content_post = get_post($my_postid); $content = $content_post->post_content; $content = apply_filters('the_content', $content); $content = str_replace(']]>', ']]>', $content); echo $content;
Ok, Now above code will mostly solve this issue but in my case it was not working and same result. I checked lots of threads in WordPress forums and stackoverflow but everyone was giving above solution in different way. So if you are getting same issue then you can use above code first to solve it but after using it your problem is not solve then below I am going to tell you one more line which you need to add in this code to solve it.
$content_post = get_post($my_postid); $content = $content_post->post_content; $content = apply_filters('the_content', $content); $content = str_replace(']]>', ']]>', $content); $content = str_replace("\r", "<br />", $content); echo $content;
So in above code you can see I added one more line before “echo $content” that line simply replacing “\r” to
"<br />"
. Now check your code. I am pretty sure it will solve your issue and your content will show with formatting.
Keyword : WordPress Get content With Formatting
I am WordPress Developer, Hire WordPress Developer India and Hire WordPress Programmer. If you have any projects related to WordPress or PHP you can contact me.
3 Comments
I have not run into this problem fetching content but good to know if I ever come across it. This is a great way for developers to create custom pages with content that can easily be changed down the road by the client. Thanks for the tutorial.
Bless you for sharing this! You saved me from a late night tantrum! 🙂
5 years later you are my hero. Thank you