WordPress get Page content By Page ID

Home » Blog » 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;

Tags: ,

28 Comments on "WordPress get Page content By Page ID"

  1. Mizan says:

    This was helpful for one of my project.

    Thanks for your help.

    Regards,
    Mizan

    Reply →
  2. Steve says:

    Is there anyway to NOT have a border box?

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

    Reply →
  4. ezhil says:

    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.

    Reply →
  5. ranouf says:

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

    Reply →
  6. ranouf says:

    The code has a problem :

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

    Reply →
  7. ZunZ says:

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

    Reply →
  8. Akilan says:

    This one really helped me.. thanks a lot

    Reply →
  9. prefonty says:

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

    Reply →
  10. ardyonline says:

    this one is really great help!
    this is cool!

    Reply →
  11. Rams says:

    Thank you!

    Just one problem, the text mark-up disappears?

    Its just raw text, so no bolded and structured text. any way to fix this? im guessing no-body will respond but hey, thats not a problem, appreciate your work sharing(: cheers

    Reply →
  12. Shamil says:

    Thanks
    Its only content of page
    How I can save design page when we call page from menu?

    Reply →
  13. meji says:

    Thanks alot dear

    Reply →
  14. shaquibkhan says:

    Thanks a lot for this code… it’s a great relief…

    Reply →
  15. Iscutearun says:

    Great help, thanks for sharing.

    Reply →
  16. Avi says:

    You can simply use get_page($page_id).

    Reply →
  17. gene says:

    gooood post, it is very useful

    Reply →
  18. peng says:

    great post, added it to my favorite

    Reply →
  19. Prasad says:

    Nice Article.

    Here you will get lot of stuff regarding to WordPress.
    Visit http://www.wordpressstop.com

    Keep Smiling.

    Reply →
  20. Nick says:

    Thanks, helpful

    Reply →
  21. Topan Aprilia says:

    thanks fot this article, i have project like this…

    Reply →
  22. Hilary says:

    THANK YOU! I had success pulling in the content from the WP reference example but I wasn’t able to truncate it. This is perfect.

    Reply →
  23. tom says:

    great post , very helpful and I’m sure that many of us will use it some day

    Reply →
  24. PowerTech says:

    Not sure how old is this post but thanks – you saved me a lot of time

    Reply →
  25. mukesh singh says:

    Thanks this is useful for me.

    Reply →

Got something to say? Go for it!

How to remove the http://t.co/XhUQ8T6p link from Login / Register Page: On self hosted wordpress blogs wordpress ... http://t.co/LWDOuEfq - 1 week ago