Limiting excerpt and title length in wordpress

Sometimes you have a web page layout that will break if the content exceeds a certain size. For example, if the excerpt of a post is too long it could break your layout. Let’s see how we can apply a word limit on the excerpt and the title.

Limit The Excerpt:

The excerpt has a straightforward way. There is a WordPress filter called excerpt_length. To limit the words to 20 words, add the following code to your functions.php located in your theme folder /wp‑content/themes/[THEME]/functions.php.

function custom_excerpt_length( $length ) {
	return 20;
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

Limit The Title:

The title is a bit trickier. There is a WordPress filter called the_title, but if we use this filter like we did with the excerpt, it will apply the limit in all of the website even on single post pages.

If we want to apply the limit on a certain page, we will have to find the location of the code responsible for displaying the title in this page. For example, in the Twenty Sixteen, the WordPress default theme, the post title code is located in the file wp‑content/themes/twentysixteen/template‑parts/content.php

When you locate the file, look for the_title() function. This function is responsible for displaying the title of the post. We need to modify the output of this function to apply our word limit

We can create a custom function to apply the limit but there is already a built-in WordPress function called wp_trim_words(). From the wordpress documentation:

This function is localized. For languages that count ‘words’ by the individual character (such as East Asian languages), the $num_words argument will apply to the number of individual characters.

Sounds good. Now let’s use it to wrap our title. First we need the title to be returned by the_title() not echoed directly which is the default. In order to do this we will pass the third argument as false. You only need to change the third argument, keep the first and the second argument as is:

the_title( '', '', false )

Now we will pass the returned title from the_title() to wp_trim_words():

echo wp_trim_words ( the_title ( '', '', false ), 4 , '...');

4: the number of words to keep
‘…’: is the characters that indicate there is more in the title. We can omit this argument here as this is the default.

Keep in mind the that any edits to theme files will be replaced if the theme is updated so it is better to create a child theme to keep your edits

4 thoughts on “Limiting excerpt and title length in wordpress”

      1. Sorry, it seems that the second code is not displayed correctly


        Hi, how to implement your code:

        ” echo wp_trim_words ( the_title ( ”, ”, false ), 4 , ‘…’); ”

        To this: ” the_title( ‘‘, ‘‘ ); “

        1. Hi Menorah,
          Sorry about the trouble you had for writing the code in your comment, WordPress is not escaping the HTML code. You need to wrap your code into <pre> tags to display it correctly
          Any way, you are trying to apply a limit by word to:

          the_title( '', '' );

          To do this you can use:

          echo wp_trim_words ( the_title ( '', '', false ), 4 , ‘…’);

          The third parameter “false” to return the title from the function. and we added “echo” to display the trimmed title.
          Replace 4 with the number of the words to keep, and replace the ‘…’ with the characters you choose to indicate there is more in the title.

          Hope this solves your problem

Leave a Reply

Your email address will not be published. Required fields are marked *