WordPress excerpt has a limited number of words by default. If you define a post excerpt upon creting your post, that entry will be printed out when you call the_excerpt() function or else, a chopped copy from your content will be printed out.

Using WordPress Filter

There is a filter that will allow you to change the excerpt length. The excerpt_length filter will let you define new length to all excerpts including custom post types. Just paste the code below into your theme’s functions.php.

if (!function_exists('ywt_new_excerpt_length')) {
    function ywt_new_excerpt_length($length) {
        return 34;
add_filter('excerpt_length', 'ywt_new_excerpt_length');

Using PHP

If you want a different approach to limit the excerpt or content length, I wrote a PHP function that will handle the task.

The Function

The function below works similarly to the above filter. However, the function will let you control the number of words separately for every post types since this function is independent to the WordPress core.

The code will also work to limit the number of words in the title which is kind of useful especially if you are dealing with designs that requires maximum number of words to keep the design even if the user inputs longer titles.

Just paste the code into your theme’s functions.php and call it wherever in your theme’s template files.

function ywt_limit_words( $string, $word_limit, $ext = null ){
    $string = html_entity_decode($string);
    $string = strip_tags($string);
    $words = explode(" ",$string);
    if(count($words) > $word_limit){
        return implode(" ",array_splice($words,0,$word_limit)) . $ext;
        return implode(" ",array_splice($words,0,$word_limit));


The function above accepts 3 parameters.

$string = (required) (string) the string you want to limit
$word_limit = (required) (int) the number of words to output
$ext = (optional) (string) the character to print after the chopped word example ‘…’ or ‘… Read More’


To use the function, just call it wherever in your theme’s template files.

if(have_posts()) :
	while(have_posts()) : the_post(); ?>

		<h3><?php echo ywt_limit_words(get_the_title(), 12, '...'); ?></h3>
		<p><?php echo ywt_limit_words(get_the_excerpt(), 20, '... Read More'); ?></p>