Skip to content

Instantly share code, notes, and snippets.

@donini
Last active September 24, 2016 03:08
Show Gist options
  • Save donini/0b044b478a92d704575b21887fdad21a to your computer and use it in GitHub Desktop.
Save donini/0b044b478a92d704575b21887fdad21a to your computer and use it in GitHub Desktop.
Limit excerpt length
<?php
function getExcerpt($str, $startPos=0, $maxLength=100) {
if(strlen($str) > $maxLength) {
$excerpt = substr($str, $startPos, $maxLength-3);
$lastSpace = strrpos($excerpt, ' ');
$excerpt = substr($excerpt, 0, $lastSpace);
$excerpt .= '...';
} else {
$excerpt = $str;
}
return $excerpt;
}
function custom_excerpt_length( $length ) {
return 20;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 5 );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment