Skip to content

Instantly share code, notes, and snippets.

@danoc
Created March 13, 2012 03:33
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 danoc/2026549 to your computer and use it in GitHub Desktop.
Save danoc/2026549 to your computer and use it in GitHub Desktop.
<?php
function get_custom_excerpt($text, $length)
{
$text = strip_shortcodes( $text );
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]&gt;', $text);
$text = strip_tags($text);
$excerpt_length = apply_filters('excerpt_length', $length);
$excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
$words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
if ( count($words) > $excerpt_length ) {
array_pop($words);
$text = implode(' ', $words);
$text = $text . $excerpt_more;
} else {
$text = implode(' ', $words);
}
return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment