Posted By

jschultz76 on 03/23/12


Tagged

wordpress


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

FlashBuddy
zemonte


Show specific WordPress Post/Page


 / Published in: PHP
 

Code used to get the content, title, excerpt, etc for a specific page or post in WordPress. Replace the post id with your own specific ID.

  1. <?php
  2.  
  3. /**
  4. * put the following into functions.php
  5. **/
  6. function new_wp_trim_excerpt($text) {
  7. $raw_excerpt = $text;
  8. if ( '' == $text ) {
  9. $text = get_the_content('');
  10.  
  11. $text = strip_shortcodes( $text );
  12.  
  13. $text = apply_filters('the_content', $text);
  14. $text = str_replace(']]>', ']]>', $text);
  15. $text = strip_tags($text, '<a>');
  16. $excerpt_length = apply_filters('excerpt_length', 55);
  17.  
  18. $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
  19. $words = preg_split('/(<a.*?a>)|\n|\r|\t|\s/', $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE );
  20. if ( count($words) > $excerpt_length ) {
  21. array_pop($words);
  22. $text = implode(' ', $words);
  23. $text = $text . $excerpt_more;
  24. } else {
  25. $text = implode(' ', $words);
  26. }
  27. }
  28. return apply_filters('new_wp_trim_excerpt', $text, $raw_excerpt);
  29.  
  30. }
  31. remove_filter('get_the_excerpt', 'wp_trim_excerpt');
  32. add_filter('get_the_excerpt', 'new_wp_trim_excerpt');
  33.  
  34. function my_excerpt($excerpt = '', $excerpt_length = 50, $readmore = "Read more&raquo;", $tags = '<a>', $permalink = null) {
  35. global $post;
  36. $excerpt = strip_tags($excerpt, $tags);
  37. $excerpt = strip_shortcodes($excerpt);
  38. $string_check = explode(' ', $excerpt);
  39. if ($permalink === null) {
  40. $permalink = get_permalink();
  41. }
  42.  
  43. if (count($string_check, COUNT_RECURSIVE) > $excerpt_length) {
  44. $new_excerpt_words = explode(' ', $excerpt, $excerpt_length+1);
  45. array_pop($new_excerpt_words);
  46. $excerpt_text = implode(' ', $new_excerpt_words);
  47. $temp_content = strip_tags($excerpt_text, $tags);
  48. $short_content = preg_replace('|\[(.+?)\](.+?\[/\\1\])?|s','',$temp_content);
  49. $short_content .= '... <a href="' . $permalink . '">' . $readmore . '</a>';
  50. return $short_content;
  51. } else {
  52. return $excerpt . ' <a href="' . $permalink . '">' . $readmore . '</a>';
  53. }
  54. }
  55.  
  56.  
  57.  
  58. ?>
  59.  
  60.  
  61.  
  62.  
  63.  
  64. <?php
  65.  
  66. /**
  67. * put the following in your template and change the post id
  68. **/
  69.  
  70. ?>
  71.  
  72. <?php $my_post_id = 495; /* change this to match the post you are trying to retreive */ ?>
  73. <?php $my_post = get_post($my_post_id); ?>
  74.  
  75. <h2><?php echo $my_post->post_title; ?></h2>
  76.  
  77. <?php $my_content = str_replace(']]>', ']]&gt;', apply_filters('the_content', $my_post->post_content)); ?>
  78.  
  79. <!-- this will output the content in excerpt format. change the '80' to whatever length you want. -->
  80. <p><?php echo my_excerpt($my_content, 80, 'Read more&raquo;', '<a>', get_permalink($my_post_id) ); ?></p>
  81.  
  82. <!-- this will output all of the content from that post/page -->
  83. <?php echo $my_content; ?>

Report this snippet  

You need to login to post a comment.