WordPress get Page content By Page ID

Recently I am working on a wordpress project where I want to display short description from clients about us page in footer..
then i find this solution to display short description from the page content. We can limit the number of words displayed.

Add following code in your functions.php file inside your theme folder.

<?php
if(!function_exists('getPageContent'))
	{
		function getPageContent($pageId,$max_char)
		{
			if(!is_numeric($pageId))
			{
				return;
			}
			global $wpdb;
			$nsquery = 'SELECT DISTINCT * FROM ' . $wpdb->posts .
			' WHERE ' . $wpdb->posts . '.ID=' . $pageId;
			$post_data = $wpdb->get_results($nsquery);
			if(!empty($post_data))
			{
				foreach($post_data as $post)
				{
					$text_out=nl2br($post->post_content);
					$text_out=str_replace(']]>', ']]&gt;', $text_out);
					$text_out = strip_tags($text_out);
					return substr($text_out,0,$max_char);

				}
			}
		}
}
?>

And to display text you have to call the function like

<?php echo getPageContent(11,150); //First parameter is PAGE ID and second is number of words displayed. ?>

OR

You can use the wordpress getPost() function to get content like

$my_postid = 12;//This is page id or post id
$content_post = get_post($my_postid);
$content = $content_post->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;

10 Comments

  1. MizanNo Gravatar
    Jan 01, 2010

    This was helpful for one of my project.

    Thanks for your help.

    Regards,
    Mizan

  2. SteveNo Gravatar
    Feb 13, 2010

    Is there anyway to NOT have a border box?

  3. pos monitoringNo Gravatar
    Feb 15, 2010

    argh, i would LOVE to be able to get this working on my site :(

  4. ezhilNo Gravatar
    Feb 26, 2010

    hi snilesh your postings are great and iam an ardent fan of your works ,i have used your plugin(wordpress news ticker),the images are not displayed even after configuring.it would be a pleasure if you help me.your plugin would be more useful if it contains some more additional features to configure.keep up your good work.

  5. ranoufNo Gravatar
    Mar 20, 2010

    Or you can use this :
    post_content;
    echo $page_data->post_content;
    ?>
    Reference : http://codex.wordpress.org/Function_Reference/get_page

  6. ranoufNo Gravatar
    Mar 20, 2010

    The code has a problem :

    $page_data = get_page(98);
    $content = $page_data->post_content;
    echo $page_data->post_content;

  7. ZunZNo Gravatar
    May 05, 2010

    Thank U very much.. I can comb my hair again :D

  8. AkilanNo Gravatar
    May 08, 2010

    This one really helped me.. thanks a lot

  9. prefontyNo Gravatar
    May 12, 2010

    Excellent work, just the doctor ordered thanks a bunch!
    Y

  10. ardyonlineNo Gravatar
    Jul 31, 2010

    this one is really great help!
    this is cool!


Trackbacks and Pings

Leave a Reply