Menu Close

Show WordPress Page Content & Title By ID With Get_Post


WordPress PageThe other day someone was asking my how to query a specific page in their WordPress theme so they could show it in the content in their site’s footer. Most the time when working with themes you most probably never query pages but more like your themes revolve around posts or custom post types, in which case you most likely use the get_posts, wp_query or the query_posts function.

To show the content of a specific page with using just the ID you’ll most likely have to use the get_post function which returns the database record for that post/page. And using the function is quite simple. Below I’ve pasted a quick snippet of a simple get_post query which you can alter to fit your needs.

$my_page_id = 69; //your page or post ID
$my_page = get_post($my_page_id); //retrieves the page via the ID
$content = $my_page->post_content; //gets the unfiltered page content
$content = apply_filters('the_content', $content); //cleanup content
$content = str_replace(']]>', ']]&gt;', $content); //cleanup content
$title = $my_page->post_title; //retrieves page title and sets the variable

echo $title; //show page title
echo $content; //show page content
View Source
Posted in WordPress