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

Comments

There are 4 comments to this post.

  1. ezhilNo Gravatar

    February 26th, 2010 at 9:54 am

    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.

  2. pos monitoringNo Gravatar

    February 15th, 2010 at 1:48 pm

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

  3. SteveNo Gravatar

    February 13th, 2010 at 12:19 am

    Is there anyway to NOT have a border box?

  4. MizanNo Gravatar

    January 1st, 2010 at 6:30 pm

    This was helpful for one of my project.

    Thanks for your help.

    Regards,
    Mizan

Leave a Comment