Multiple excerpt lengths in WordPress

The default WordPress excerpt length is 55 words. Sometimes we need to change this, and sometimes we need multiple excerpt lengths depending on where we are. For example, we might want a short excerpt on the home page and the default excerpt for everywhere else – archives, author pages, etc.

function jp_multiple_excerpt_lengths($length) {
	if ( is_front_page() ) {
		return 15;
	}

	return 55;
}
add_filter( 'excerpt_length', 'jp_multiple_excerpt_lengths' );

The example above changes the excerpt length on the front page, but WordPress comes with numerous conditional tags we can use in our themes, like is_category and is_archive.

We can even add extra conditional cases to the code above. Let’s say we want a short excerpt on the front page, a longer one on author pages, and the default everywhere else.

function jp_multiple_excerpt_lengths($length) {
	if ( is_front_page() ) {
		return 15;

	} elseif ( is_author() ) {
		return 65;

	} else {
		return 55;

	}
}
add_filter( 'excerpt_length', 'jp_multiple_excerpt_lengths' );

Leave a Reply