Skip to content

Instantly share code, notes, and snippets.

@lmartins
Created January 16, 2015 19:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lmartins/1f38a4e942a02c5e4dba to your computer and use it in GitHub Desktop.
Save lmartins/1f38a4e942a02c5e4dba to your computer and use it in GitHub Desktop.
Limit the excerpt lenght without truncating words
/**
* ----------------------------------------------------------------------------
* LIMITAR EXCERPT
* Limita o excerpt sem cortar palavras a meio
* http://wordpress.stackexchange.com/questions/70913/how-can-i-limit-the-character-length-in-excerpt
* ----------------------------------------------------------------------------
*/
function get_excerpt($limit, $source = null){
if($source == "content" ? ($excerpt = get_the_content()) : ($excerpt = get_the_excerpt()));
$excerpt = preg_replace(" (\[.*?\])",'',$excerpt);
$excerpt = strip_shortcodes($excerpt);
$excerpt = strip_tags($excerpt);
$excerpt = substr($excerpt, 0, $limit);
$excerpt = substr($excerpt, 0, strripos($excerpt, " "));
$excerpt = trim(preg_replace( '/\s+/', ' ', $excerpt));
$excerpt = $excerpt.'... <a href="'.get_permalink($post->ID).'">more</a>';
return $excerpt;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment