Nov 1, 2008 - PHP, Wordpress    48 Comments

WordPress: get page content by page id. Static pages to Dynamic


Recently I am working in a wordpress project where I am converting a total static site to wordpress site. What I do is: (1) from the static pages, I am taking the static text and creating a new wordpress page for each static page. (2) fetching the page content/text by page id and (3) showing them wherever I want.

I have written a function and now I can fetch the content of any page or one more pages just by the page id.

I am using this function to fetch several page data and show them in one page. In the static site, there are several section with different designs. The client want edit each section using wordpress. So, if there are three different sections, I am creating three individual pages for this single page. Next, I am just fetching the content of the three pages by calling my method three times with different parameters and showing the output in one page.

For exampe,

Thus, I am just shifting all text and/or content of the static site to wordpress so that the client can edit the site himself. I think this is a simple way or technique if you want to convert your static page to dynamic wordpress site.

You can also customize this one as per your need. For example, you may only need to fetch the page title.

You can not only get the page content, but also the post content if you set the value of the “$pageId” equals any post id. You know you can see the page or post id from the admin panel.

By the way,
I have put my functions in the functions.php file so that I can access it from anywhere. I suggest you following the same way i.e. writing all your custom functions in the functions.php file.

Thank you for reading.


  • thanks for this template! i’ve been looking everywhere for just this fix. i tried copying and pasting your function in my functions.php and an error comes up saying, “Parse error: syntax error, unexpected T_STRING”. here’s what my functions.php looks like:

    posts .
    ‘ WHERE ‘ . $wpdb->posts . ‘.ID=‘ . $pageId;
    $posts = $wpdb->get_results($sql_query);
    foreach($posts as $post)
    return nl2br($post->post_content);

    any thoughts?

  • @ timmyp

    listen…delete and type the single quotation marks (‘)
    because this(‘) this often cause this problem!

    or the best way is to type the whole thing yourself.
    but i think if you delete the (‘) and write again, it should solve your problem.

  • Thank you very much!
    This is exactly what I was looking for.
    also the quotation marks solved my problem.

  • Bro,
    I’ve tried different codes; I’ve tried my own. Now I’m trying yours. Yours gives me the closest result to what I’m looking for. The only issue I am having is that when the page being called comes up, it actually show the code of the page, and not rendering. Ain’t that weird?. I see the actual code of the page being called. It actaully shows the CSS formatting too, but with the php code on top of it.
    I thought I was getting an error in the page executing the function, but it is actually showing the code for the page being called. any ideas?. Thanks.

  • @ soccermatrix

    I have an easier solution in other post.
    Try this out:

    This should solve your problem.

  • Hi,

    i think this is exactly what i need:

    but where do i have to put these things and where to put in the page id?

    I don’t understand a word sorry 😀

  • @ pcuserman

    If you give a second look, it think you will get your answer.

  • no, seriously please give me a hint 😉

  • @ pcuserman

    If you are not used to wordpress custom coding, it is bit tough to explain.

    Better you check this link for an easier solution:

  • Here is more elegant and combination of both functions into one.
    function getPage($pageId, $whatToGet = ‘content’)
    if(!is_numeric($pageId)) { return; }
    global $wpdb;
    $whatToGet = $whatToGet == ‘title’ ? ‘post_title’ : ‘post_content’;

    $return = $wpdb->get_var(“SELECT $whatToGet FROM $wpdb->posts WHERE ID = ‘$pageId'”);
    if(!empty($return)) { return $return; }

  • It works great but I get errors (related to the functions) when trying to login/logout of wp-admin page

    Warning: Cannot modify header information – headers already sent by (output started at /Library/WebServer/Documents/ in /Library/WebServer/Documents/

    Anyone have a solutio for this??

  • This function gets one row and returns the object as an array.

    In my template file I can do:
    $weclome_block = getPageBlock(66);

    And I have the date strings to format if I need the date, or last modified.

    if(!function_exists(‘getPageBlock’)) {
    function getPageBlock($pageId) {
    if(!is_numeric($pageId)) {
    global $wpdb;
    $sql_query = “SELECT DISTINCT * FROM “.$wpdb->posts .” WHERE ID=$pageId”;
    $my_block = $wpdb->get_row($sql_query, ARRAY_A);
    if(!empty($my_block)) {
    return $my_block;
    } else {

  • I think there is a direct function in the WP core for this purpose…
    Check this out…

  • I have been searching for a way to put multiple editable content on my WordPress site, and all I’ve been finding are tutorials on how to put multiple posts on pages. Your solution is perfect for my site. It’s going to be very simple to set up the whole site so the client can maintain the content. Thank you for a great script.

  • Nice functions.

    I suggest changing out the nl2br on the return and using the WordPress function wpautop() instead. This is like nl2br but formats tags as well. This will help replicate the_content() even better…

  • Thanks heaps for this stuff, it works really well.
    I am using this function to return the post_modified date
    to the homepage to show the last time the news page was updated.

    The problem is I am trying to alter the format that the date is displayed in,

    it is currently like this 2009-09-29 01:35:42,

    it needs to be like this 06 September 2009.

    I’m very new (ie: coupla weeks) to php and wordpress,

    Any help greatly appreciated!

  • How could I receive page content by ID, if my content include some plugin text, like [googleMap]map[/googleMap], And I need not the text ‘[googleMap]map[/googleMap]’, but I need google map at my page. Can some one help me?

  • YEAH! nice function dude, that helped me out a whole lot. The fact that it doesn’t disturb the loop is just great! cheers, have a great day.

  • GREAT resource!
    Thanks for all your efforts and keep up the good work!

  • Hi, great function. Is it possible to make the content linkable back to the page it was taken from?


  • Hi,
    the called page content replaces by . Is there a way to keep the original style ?


  • i have no php knowledge whatsoever that said, how would i turn this into a shortcode where i could just enter the page id within a post or page??


  • I actually have a slight change to your function that would actually be a huge improvement. Put this in your functions.php.

    posts .
    ‘ WHERE ‘ . $wpdb->posts . ‘.ID=’ . $pageId;
    $posts = $wpdb->get_results($sql_query);
    if(!empty($posts)) {
    foreach($posts as $post) {
    return $post;

    And use it like this:

    Then you can get any information you want about the post including title, ID, content, etc.

  • I guess it would be nice if I could post code. -_-

  • Great function. Using it to put the content of a page into a jQuery drop down panel I created on my site. I was working my way through the WordPress loops (none of which worked the way I wanted) and found your post.
    A big thanks.

  • This is great! One question – how would I tweak this to display an excerpt of a page or child page?


  • Thanks a lot for this, you saved my life!! 😉

  • I agree with Edward. This is perfect. I have a site that we still want complete graphic control over without getting into template style building. I can’t tell you how many hours have been spent reading the complete abuse of the word “integrate wordpress with static page” or “integrate wordpress with website backend only” etc, just to find pages explaining how to…build a theme, build a template, convert entire site essentially over to wordpress.

    I’ve been building custom sites for over a decade and we build admins for key components that need to be dynamic (calendar, photogallery, audio systems and video) but they are Extremely custom each time and having side skills at wordpress theme rebuilds etc, they are two different worlds when you search in Google, etc.

    So, to my friend in Dhaka Bangladesh how has given this tutorial to use,
    Ozasro Dhonnobad

  • Thanx. I was looking for this way. It is more helpful and right way to get content of any page. Thanx again…

  • good ismail..

  • thanks for this solution
    what about WPML plugin?

  • @ ricorp

    I do not know about the WPML plugin.

  • Now i use this for WPML

    $content = apply_filters(‘the_content’, $content);
    $content = str_replace(‘]]>’, ‘]]>’, $content);
    echo $content;
    else{if(ICL_LANGUAGE_CODE ==’fr’){
    $my_postid = x;
    $content_post = get_post($my_postid);
    $content = $content_post->post_content;
    $content = apply_filters(‘the_content’, $content);
    $content = str_replace(‘]]>’, ‘]]>’, $content);
    echo $content;

  • if(ICL_LANGUAGE_CODE ==’en’){
    $my_postid = 189;
    $content_post = get_post($my_postid);
    $content = $content_post->post_content;
    $content = apply_filters(‘the_content’, $content);
    $content = str_replace(‘]]>’, ‘]]>’, $content);
    echo $content;

  • else{if(ICL_LANGUAGE_CODE ==’fr’){
    $my_postid = 191;
    $content_post = get_post($my_postid);
    $content = $content_post->post_content;
    $content = apply_filters(‘the_content’, $content);
    $content = str_replace(‘]]>’, ‘]]>’, $content);
    echo $content;

  • thank you very much..
    I need to limit the characters, can yu help me please??

  • @ luay

    you should use string function.
    you can search in and will probably find a solution.

  • Thanks, was looking for something just like this to display different content to users that are logged-in as opposed to guests. Works perfectly for what I needed.

  • This works great but how do you get shortcodes to display content this way? For example, I have a single page design pulling content from various pages I set up for my client to edit. When adding a shortcode to those pages, getPageContent just spits out the code.

  • Throw a syntax error for me…

  • Nice Post
    This is very helpful to me on my development time.

    Thanks for sharing us

  • Appreciate your taking the time to share this. Was looking for a way to check for a string match in a page/post to determine if other code should be fired or not and was using the_content but it was throwing off some themes. This seems to fit the bill.


  • Instead of nl2br when returning the content I used wpautop. It worked alot better for me.

  • When using with qTranslate this function returns all contents of all activated languages (one after another in same div) – how to return only content of one actual language?

  • Really great post. This post is really helpful fro me as i work on WordPress.
    Thanks for share.

  • For more than 8hrs i was all over WordPress codex looking for something to pull a post content to a template, you made my life a little more fruitful.

  • Great post. This is exactly what been looking for.

    Thanks this is a big help for me 🙂

  • I keep getting this issue when logging into admin…

    Warning: Cannot modify header information – headers already sent by (output started at /home/content/77/8228177/html/sandbox/wp-content/themes/singleinstilettos/functions.php:114) in /home/content/77/8228177/html/sandbox/wp-includes/functions.php on line 862

    Can anyone tell me how to fix this? Doesn’t seem to break the site at all, but I just wanted to see if it can be fixed.

Got anything to say? Go ahead and leave a comment!