Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Is there a way to limit the word count of the post title?

I searched over the internet but found nothing.

All I know is, only the content of the post can be limited or excerpted.

share|improve this question

3 Answers 3

There is a built-in function for that: wp_trim_words().

add_filter( 'the_title', 'wpse_75691_trim_words' );

function wpse_75691_trim_words( $title )
{
    // limit to ten words
    return wp_trim_words( $title, 10, '' );
}
share|improve this answer
function limit_word_count($title) {
    $len = 5; //change this to the number of words
    if (str_word_count($title) > $len) {
        $keys = array_keys(str_word_count($title, 2));
        $title = substr($title, 0, $keys[$len]);
    }
    return $title;
}
add_filter('the_title', 'limit_word_count');

You can put any kind of limit on almost anything you like, you just need the correct filter

share|improve this answer

simply use this where ever you want to display your title with limited words

<?php echo wp_trim_words( get_the_title(), 5 ); ?>

replace number 5 in above code with whatever number of words you need to display.

Regards.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.