Skip to content

Instantly share code, notes, and snippets.

@kjbrum
Last active May 21, 2016 02:18
Show Gist options
  • Save kjbrum/5d90663036d64e5f5c9f to your computer and use it in GitHub Desktop.
Save kjbrum/5d90663036d64e5f5c9f to your computer and use it in GitHub Desktop.
Create a custom length excerpt.
<?php
/**
* Create a custom length excerpt.
*
* @param integer $limit How many words you want returned
* @return string $content The content string
*/
function wp_excerpt_length( $limit=50 ) {
$content = get_the_content();
$content = preg_replace( '/\[.+\]/', '', $content );
$content = apply_filters( 'the_content', $content );
$content = str_replace( ']]>', ']]&gt;', $content );
$content = strip_tags( $content );
$content = explode( ' ', $content, $limit );
if( count( $content) >= $limit ) {
array_pop( $content );
$content = implode( ' ', $content ) . '...';
} else {
$content = implode( ' ', $content );
}
return $content;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment