WordPress: get page id and content with example & code

Hey Guys!

Often I see people coming to my site searching with the terms wordpress get page id, wordpress get page content or something similar. I think wordpress developers face situations when they need to get page or post information in customized way. It happened to me too and I would like to share it with others since often people are coming to search this information.

Well. It is very easy and we can solve it quickly.
OK. Now let me show you how to get these information.

Remember one thing that is important for wordpress data fetching of this kind. Your posts and pages information is saved in a single database table and that is wp_posts. The wp_ is the prefix of you database and may differ. But most of the times the database table name is wp_posts and other times it is YourCustomPrefix_posts. We are going to fetch data from this table.

WordPress – print page id:

There is a built-in wordpress function using which you can print the post or page id. When you call this, this directly prints this inforation in your page without the need to use the built-in PHP echo or print function. Remember to keep it in the while loop.

{code type=PHP}


{/code}

WordPress – get page id:

There is a global variable post which contains the related information of the currest post or page. The name of the variable is: $post and it is actually an object. You can access information just as you access variables from an object. Remember to keep it in the while loop.

{code type=PHP}

global $post;
echo $post->ID;
?>


{/code}

You can print all the information in the $post object to see all the variables and their values that is contains.

{code type=PHP}

global $post;
var_dump($post);
?>


{/code}

WordPress – get page content:

Now you know you can get the page or post content from $post->post_content variable. But if you echo or print them, they may look somewhat without formatting. So, you need to use PHP built-in nl2br() function to look the content as it is.

{code type=PHP}

global $post;
echo nl2br($post->post_content);
?>


{/code}

WordPress – get any information of your page or post:

You know you can print all information to check all the available variables and their values through using PHP’s built-in var_dump() function. Suppose you need to get the post title, post type and posting time. We can get them easily in this way.

{code type=PHP}

global $post;
echo $post->post_title;
echo $post->post_type;
echo $post->post_date;
?>


{/code}

Please notice that post_title, post_type and post_date are all several database fields from wp_posts table of our wordpress database.

Very easy. Right? And that is all for getting the page or post information.

Thank you for reading.

45 thoughts on “WordPress: get page id and content with example & code

  1. Thanks for the info. I have been looking for how to add page names as ids to the body of the design so I could implement a CSS sprite navigation. This was superhelpful.

  2. I’m trying to use the Included Page plugin which pulls in data from another page by using PHP, (written as iinclude_page(pageid);) but for that data to be different depending on what page it is on.
    Now, what I think I should do is have the code do something like this, but I dont know what the variables that identify what the page we’re looking at would be:

    if ((thispageis == ‘pageidentifier’) {
    ?> iinclude_page(4); iinclude_page(5);
    }

    Could you help me figure out what the variables would be?
    Thanks!

  3. The reason for this is that the sidebar needs to change depending on what page a person is on, and those sidebars also need to remain editable through the WP editor in the admin area – the main backend user knows no code.

  4. I tried the “WordPress – get page content:” function but when I loaded the page it gave me an error. “Parse error: syntax error, unexpected T_ENDIF in ”
    This function was included on a seperate page from the main index.php page.
    Could this be the problem I am having with this function? I’ve been trying to find out how to call other posts that have been written into my Recipes.php page. I’ve looked all over but with no luck. Any help you can give would be very appreciated! Thanks.

  5. @ Sparkyjoe

    delete the single quotes(‘) and double quotes(“)
    and manually type them.

    Often copying and pasting the code in editor does not work because of single and double quotes problems.

    Once again delete them and type them in proper way.
    This should solve the problem.

  6. I figured out my problem, well one problem. I founf that I had an extra
    . I deleted it and that atleast displayed the page that i was wanting but I was thinking it would display the titles and content to all the pages I was calling.
    Is there any way to make multiple pages and have them displayed like posts on a seperate page that I specify without displaying them on my main index.php page?

  7. @ Sparkyjoe

    Yes.
    It is possible.
    It depends how you do it.

    You can write a function and pass post ids.
    Thus, you can get multiple posts’ information.

  8. ok, last try:

    i’ve got a homepage/index.html.
    In that the content of wordpress/?page_id=11 shall be displayed.

    Where do i have to put the function? in the template of wordpress or in the index.html?

    And how to call the content in the html?

  9. @ pcuserman

    In an HTML file, you can never put dynamic content
    first of all, you must make it a .php file.

    download the default wordpress theme and examine the files.
    so, you will get a better answer to your question

    also you can take a look at my other posts for a solid idea for your question’s answer:
    http://www.tanzilo.com/category/wordpress/

  10. too hard to give a quick solution, huh?

    sorry, but instead of telling me what to do you easily could have told me in 3 steps where to put these functions in^^

  11. @ pcuserman

    keep your functions in functions.php file in your theme folder and you can access it from anywhere

  12. Thank you for the great, post it has been much appreciated and implemented into our upcoming website due to be released by Thursday 19th November 2009.
    It’s a nice work around for the non-working ‘get_page();’ function.

  13. what if a page is used to house the posts. will the page id of that page change? this usually the case when using a static page for homepage. doing so won’t let you call that page thru is_page() using its id.

  14. @ potobenka

    there is a function wp_list_pages

    also there are many other ways such as custom coding to get the posts!

  15. Hi, can you help me please ?
    example :
    I have wordpress page with name :
    http://127.0.0.01/mywordpress/?page_id=67

    I want the content of that page can appear in my home page.
    Only content from that page (Not Tittle or etc).

    so, what the php code must put in my div table ?
    I try using your code above, but all contents of the posts appear in my div table at home page.

  16. sorry, I am not familiar with php code.
    I has seen your link reference, but I still confuse.
    can you please give me the code for make appear the page above like I told ? So, I can put the code in my table.
    Thank you.

  17. Thanks that rules! I knew I’d learned this before….stumbled around google then came (back) to your site. I knew this is where i’d learned it b’c you have a beautiful site.

    Thanks!

  18. Hi, thanks for this interesting post!
    I have one more question: Is ther a way to display in a sitemap the page title + page ID?

    I’m using this line to display all pages + subpages:

    And I’d like to have the output like
    “Home – 1″
    “Page XYZ – 2″
    ect.

    Is there a way to do this?
    Thanks!

  19. @ Mandy

    You can write a custom function in the functions.php file where you can show the pages and ids something like:

    < ?php

    function getSitemap()
    {
    global $wpdb;
    $output = '';
    $sqlQuery = "SELECT * FROM {$wpdb->prefix}posts
    WHERE post_status = ‘publish’
    AND post_type=’page’
    ORDER BY post_date DESC; “;
    $dbResult = mysql_query($sqlQuery);
    while($dbRow = mysql_fetch_object($dbResult))
    {
    $output .= $dbRow->post_title . ‘ – ‘ . $dbRow->ID . ‘
    ‘;
    }
    return $output;
    }

    ?>

    Then you call it in your sitemap Template file:
    < ?php getSitemap(); ?>

    Give a try!

  20. Great post!
    I had this problem, where I wanted to show something on all pages but the first page. Very easy after I read your post and know how to get the ID of the first page.

    Thank you SO much!

  21. Hello!
    This is great! Thanx!

    I have one question, maybe you can help me. I keep trying things but still no success!

    I got four boxes on my home page, and in each one, my client wants now to display content from 4 static pages. I am using the following code to do that:

    post_excerpt;
    $title = $page_data->post_title;
    echo $page_data->post_title;
    echo $page_data->post_excerpt;
    ?>

    They want also to have the featured image to display in the box, together with the title and the excerpt. How can I achieve that?

    I can’t have the content to display, because they want the image to stay a fix size, and the page content might have more than one image, but only the featured one should show up in the home page.

    Thank you!

    Chiara

  22. your function is not great as it cutting off the paragraph tags of the content putting instead . nl2br is what is causing it.

    Use

    post_content); echo $content; ?>

    instead.

  23. please help me i trying to get different post content according to there ID but i am unable to get either title or content in the

    Please how can i fix this help me. If its possible to reply me on my email then that would be much better nehul.splendit@gmail.com

    9, ‘category’ => 7 );
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) : setup_postdata($post); ?>
    ID ), ‘single-post-thumbnail’ ); ?>
    ID); ?>

    <!–<img alt="Portfolio Item" src="/images/portfolio_images/9.png” class=”darkBorderFrame” />–>

    <a rel="group_fancybox" title="”
    href=”” class=”icon_expose”>

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>