Format post_content From get_page() and get_post() in WordPress
In WordPress it’s possible to output the content of the current page by doing the following:
Sometimes however, we might to get and display content from another page or post. A solution to this is to use the get_page() or get_post() functions and output the content that gets returned. Let’s take a look at what this might look like:
$page = get_page(4); echo $page->post_content;
In the above snippet of code I’m getting all information relating to the page with ID 4, then outputting the content.
This works, however what you’ll notice is that the content displayed doesn’t contain any formatting (ie. no paragraphs).
Luckily, the solution is easy and simply requires us running the content through the filter that applies the formatting. Let’s take a look at how we do this by taking the code above and amending it:
$page = get_page(4); echo apply_filters('the_content', $page->post_content);