Created
December 12, 2012 08:02
-
-
Save dragunoff/4265962 to your computer and use it in GitHub Desktop.
[WP] Generate an excerpt from any text
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
<?php | |
/** | |
* Generate an excerpt from any text. | |
* | |
* @author Ivaylo Draganov <[email protected]> | |
* @note A modified version of core WP function wp_trim_excerpt | |
* @param (string) $text - The text to trim. | |
* @param (int) $words - The number of words to trim. | |
* @return (string) The trimmed and filtered excerpt. | |
*/ | |
function custom_trim_excerpt( $text = '', $words = '' ) { | |
$raw_excerpt = $text; | |
if ( $text ) { | |
$text = strip_shortcodes( $text ); | |
$text = apply_filters('the_content', $text); | |
$text = str_replace(']]>', ']]>', $text); | |
$excerpt_length = ( $words && is_numeric( $words ) ) ? absint( $words ) : apply_filters('excerpt_length', 55); | |
$excerpt_more = apply_filters('excerpt_more', ' ' . '[...]'); | |
$text = wp_trim_words( $text, $excerpt_length, $excerpt_more ); | |
} | |
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