Learning Is Fun

Talks on Web Technology and Better Product Development

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

November1

Hello!

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.

<?php
	if(!function_exists('getPageContent'))
	{
		function getPageContent($pageId)
		{
			if(!is_numeric($pageId))
			{
				return;
			}
			global $wpdb;
			$sql_query = 'SELECT DISTINCT * FROM ' . $wpdb->posts .
			' WHERE ' . $wpdb->posts . '.ID=' . $pageId;
			$posts = $wpdb->get_results($sql_query);
			if(!empty($posts))
			{
				foreach($posts as $post)
				{
					return nl2br($post->post_content);
				}
			}
		}
	}
?>

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,

<div id="income_tax">
	<?php echo getPageContent(6); ?>
</div>
<div id="tax_advise">
	<?php echo getPageContent(7); ?>
</div>
<div id="yearly_return">
	<?php echo getPageContent(8); ?>
</div>

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.

<?php
	if(!function_exists('getPageTitle'))
	{
		function getPageTitle($pageId)
		{
			if(!is_numeric($pageId))
			{
				return;
			}
			global $wpdb;
			$sql_query = 'SELECT DISTINCT * FROM ' . $wpdb->posts .
			' WHERE ' . $wpdb->posts . '.ID=' . $pageId;
			$posts = $wpdb->get_results($sql_query);
			if(!empty($posts))
			{
				foreach($posts as $post)
				{
					return nl2br($post->post_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.

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
posted under Blog, PHP, Wordpress
26 Comments to

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

  1. On December 8th, 2008 at 10:53 am timmyp Says:

    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);
    if(!empty($posts))
    {
    foreach($posts as $post)
    {
    return nl2br($post->post_content);
    }
    }
    }
    }
    ?>

    any thoughts?

  2. On December 8th, 2008 at 11:03 am admin Says:

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

  3. On March 17th, 2009 at 7:08 am The New Rich Says:

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

  4. On March 22nd, 2009 at 4:47 pm soccermatrix Says:

    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.

  5. On March 22nd, 2009 at 4:53 pm admin Says:

    @ soccermatrix

    I have an easier solution in other post.
    Try this out:
    http://www.tanzilo.com/2008/12/19/wordpress-get-page-id-and-content-with-example-code/

    This should solve your problem.
    :)

  6. On March 24th, 2009 at 2:58 pm pcuserman Says:

    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 :D

  7. On March 24th, 2009 at 3:04 pm admin Says:

    @ pcuserman

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

  8. On March 24th, 2009 at 3:08 pm pcuserman Says:

    no, seriously please give me a hint ;)

  9. On March 24th, 2009 at 3:10 pm admin Says:

    @ pcuserman

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

    OK.
    Better you check this link for an easier solution:
    http://www.tanzilo.com/2008/12/19/wordpress-get-page-id-and-content-with-example-code/

  10. On May 19th, 2009 at 2:09 am Beysim Says:

    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; }
    }

  11. On May 28th, 2009 at 2:00 am Rich Says:

    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/hotexistence.com/wp-includes/functions.php:2919) in /Library/WebServer/Documents/hotexistence.com/wp-login.php

    Anyone have a solutio for this??

    http://www.hotexistence.com

  12. On June 25th, 2009 at 5:42 am Chicago Web Design Says:

    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)) {
    return;
    }
    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 {
    return;
    }
    }
    }

  13. On July 3rd, 2009 at 5:26 am Arif Uddin Says:

    I think there is a direct function in the WP core for this purpose…
    Check this out…
    http://codex.wordpress.org/Function_Reference/get_page

  14. On September 26th, 2009 at 11:26 pm Patrick Says:

    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.

  15. On September 29th, 2009 at 3:21 am ben Says:

    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…

  16. On September 29th, 2009 at 3:01 pm Tim Says:

    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!

  17. On October 15th, 2009 at 4:24 am Eugene Says:

    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?

  18. On November 22nd, 2009 at 6:18 pm elliot condon Says:

    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.

  19. On January 13th, 2010 at 8:13 pm Ciprian Says:

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

  20. On February 4th, 2010 at 4:59 am Ronny Says:

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

    thanks!

  21. On February 16th, 2010 at 7:28 am Quentin Says:

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

    X

  22. On March 16th, 2010 at 9:54 pm audrey driver Says:

    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??

    thanks!!
    -a

  23. On March 19th, 2010 at 11:41 am Mvied Says:

    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:
    post_content);
    ?>

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

  24. On March 19th, 2010 at 11:42 am Mvied Says:

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

  25. On March 29th, 2010 at 8:02 am Tony Boyce Says:

    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.

  26. On July 6th, 2010 at 9:35 am Alex Says:

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

    Thanks!

Email will not be published

Website example

Your Comment: