Format post_content From get_page() and get_post() in WordPress

June 20th, 2013 - Posted by Steve Marks to PHP, Web Development.

In WordPress it’s possible to output the content of the current page by doing the following:



echo get_the_content();

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).

The Solution

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);
This entry was posted on Thursday, June 20th, 2013 at 7:20 am by +Steve Marks and is filed under PHP, Web Development. You can follow any responses to this entry through the RSS 2.0 feed.

Fear not, we won't publish this

Comments (0)

No comments have been left yet. Be the first