Last active
September 24, 2016 03:08
-
-
Save donini/0b044b478a92d704575b21887fdad21a to your computer and use it in GitHub Desktop.
Limit excerpt length
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 | |
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