Posted By

scitrenbaumgmailcom on 07/12/10


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

lanceosborne


Wordpress - Get page content by Page ID


 / Published in: PHP
 

  1. Add following code in your functions.php file inside your theme folder.
  2.  
  3. <?php
  4. if(!function_exists('getPageContent'))
  5. {
  6. function getPageContent($pageId,$max_char)
  7. {
  8. if(!is_numeric($pageId))
  9. {
  10. return;
  11. }
  12. global $wpdb;
  13. $nsquery = 'SELECT DISTINCT * FROM ' . $wpdb->posts .
  14. ' WHERE ' . $wpdb->posts . '.ID=' . $pageId;
  15. $post_data = $wpdb->get_results($nsquery);
  16. if(!empty($post_data))
  17. {
  18. foreach($post_data as $post)
  19. {
  20. $text_out=nl2br($post->post_content);
  21. $text_out=str_replace(']]>', ']]&gt;', $text_out);
  22. $text_out = strip_tags($text_out);
  23. return substr($text_out,0,$max_char);
  24.  
  25. }
  26. }
  27. }
  28. }
  29. ?>
  30.  
  31.  
  32. And to display text you have to call the function like
  33.  
  34. <?php echo getPageContent(11,150); //First parameter is PAGE ID and second is number of words displayed. ?>

Report this snippet  

You need to login to post a comment.