WordPress: get page content by page id. Static pages to Dynamic
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.
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?
@ 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:
http://www.tanzilo.com/2008/12/19/wordpress-get-page-id-and-content-with-example-code/
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.
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/
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/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
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;
}
}
}
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
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?
thanks!
Hi,
the called page content replaces by . Is there a way to keep the original style ?
X
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
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.
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!