Last active
August 29, 2015 13:57
-
-
Save roryashfordbentley/9663538 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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