WordPress – Get Page or Post Content By ID

Often when creating your homepage or a featured content page or slider you will need to get content from specific pages in your site. The following code snippet show a quick and easy way to do this, based on the ID of the page, post or custom post type.

<?php 
$page_id = 6;  //Page ID
$page_data = get_page( $page_id ); 

//store page title and content in variables
$title = $page_data->post_title; 
$content = apply_filters('the_content', $page_data->post_content);
//display the title and content
echo $title;
echo $content; 
?>

Rinse and repeat to include content from multiple pages.

If you need to get the page ID you can easily do this by logging into your WordPress website admin and opening the page, post or custom post type for editing. The page id is visible in the address bar as shown in the screenshot below.



Like this story? You may also be interested in:

2 Responses to “WordPress – Get Page or Post Content By ID”

  1. nollaig says:

    mark, u ever here of a company called Leiki, your piece above put them into my head, they return a list of relevant content to teh user based on the web page your on….its actually very interesting what they do and how they do it…….media companies would use them, that whole space of accessing user preference is blowing up…

Leave a Reply