Skip to content

Instantly share code, notes, and snippets.

@WebEndevSnippets
Created November 6, 2012 13:54
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 WebEndevSnippets/4024867 to your computer and use it in GitHub Desktop.
Save WebEndevSnippets/4024867 to your computer and use it in GitHub Desktop.
WordPress: Limit Length of All Excerpts (Manual and Automatic)
remove_filter( 'get_the_excerpt', 'we_trim_excerpt' );
add_filter( 'get_the_excerpt', 'we_trim_all_excerpt' );
/**
* Limit Length of All Excerpts (Manual and Automatic)
*
*/
function we_trim_all_excerpt($text) {
// Creates an excerpt if needed; and shortens the manual excerpt as well
global $post;
$raw_excerpt = $text;
if ( '' == $text ) {
$text = get_the_content('');
$text = strip_shortcodes( $text );
$text = apply_filters( 'the_content', $text );
$text = str_replace( ']]>', ']]>', $text );
}
$text = strip_tags($text);
$excerpt_length = apply_filters( 'excerpt_length', 45 );
$excerpt_more = apply_filters( 'excerpt_more', ' ' . '&nbsp;<a class="more-link" href="'.get_permalink().'">[More&#x2026;]</a>' );
$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