Skip to main content

Set Excerpt Length

PropertyValue
descriptionTruncate WordPress excerpts to a fixed length.
tagslib, php, wp, oop
rating

Overview

Set a fixed word count for automatically generated WordPress excerpts. The snippet hooks into excerpt_length and returns 40, replacing WordPress' default excerpt length.

Change the returned number when a theme needs a different excerpt length.

Variants

<?php
/**
* Set excerpt length.
*/
if ( ! function_exists( 'mac_set_excerpt_length' ) ) {

function mac_set_excerpt_length( int $length ): int {
return 40;
}
}

add_filter( 'excerpt_length', 'mac_set_excerpt_length', 10, 1 );