Tuesday 14 May 2013

Get excerpt by post id in wordpress

// Function add in function.php

function get_excerpt_by_id($post_id){
   $the_post
= get_post($post_id);
   $the_excerpt
= $the_post->post_content;
   $excerpt_length
= 35;    //Sets excerpt length by word count
   $the_excerpt
= strip_tags(strip_shortcodes($the_excerpt));
   $words
= explode(' ', $the_excerpt, $excerpt_length + 1);

   
if(count($words) > $excerpt_length) :
       array_pop
($words);
       array_push
($words, '…');
       $the_excerpt
= implode(' ', $words);
   endif
;

   $the_excerpt
= '<p>' . $the_excerpt . '</p>';

   
return $the_excerpt;
}

// Call in template files

echo get_excerpt_by_id($post_id);   // you will get excerpt by post id

No comments:

Post a Comment

Thanks for visit blog.