How to add a custom excerpt length to WordPress pages and posts?


The WordPress excerpt is set by adding / modifying within the functions.php file.
However, what if i wanted a different excerpt length for different pages? You could create a series of IF/ELSE or Switch statements but this wouldn’t be the most efficient.


  1. Add the following to your functions.php within your template folder.

    function excerpt($limit) {
    	$excerpt = explode(' ', get_the_excerpt(), $limit);
    	if (count($excerpt)>=$limit) {
    		$excerpt = implode(" ",$excerpt).'...';
    	} else {
    		$excerpt = implode(" ",$excerpt);
    	$excerpt = preg_replace('`[[^]]*]`','',$excerpt);
    	return $excerpt;
  2. Then you can use excerpt(80) in your template files.
    Nice, simple and easy which will help you speed up development time.