Skip to content

Instantly share code, notes, and snippets.

@roryashfordbentley
Last active August 29, 2015 13:57
Show Gist options
  • Save roryashfordbentley/9663538 to your computer and use it in GitHub Desktop.
Save roryashfordbentley/9663538 to your computer and use it in GitHub Desktop.
function get_excerpt_by_id( $post_id, $charlength = 140, $ending = '[…]' ) {
global $post;
if ( $post->ID !== $post_id )
$post = get_post( $post_id );
if ( has_excerpt( $post->ID ) )
return $post->post_excerpt;
$subex = mb_substr( $post->post_content, 0, $charlength - 5 );
$exwords = explode( ' ', $subex );
$excut = - ( mb_strlen( $exwords[ count( $exwords ) - 1 ] ) );
if ( $excut < 0 ) {
$subex = mb_substr( $subex, 0, $excut );
}
return $subex . $ending;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment