Home  »  ArticlesGuidesProgrammingTechnologyTips   »   So you Want to Change The Default Excerpt Length In WordPress

So you Want to Change The Default Excerpt Length In WordPress

Now that you are deep into customizing your WordPress themes, there begs the question of how to customize the default excerpt length.

The thing is, WordPress comes with a default excerpt length of 55 words. Depending on what you want your theme to look like you may want to increase this word count or more often than not reduce it entirely.

The challenge here comes in how to get it done. Many will probably tell you to just search for, download, and install a plugin, and hey presto, it’s done.

That may work to some extent but it poses some other problems. These include risking security in case the plugin is compromised. The plugin developer may also change some terms in the future which may no longer favor you. The plugin developer may also decide to push ads through the plugin.

The Easier way to Change the WordPress Default Excerpt Length

With just four lines of code, you can do the same thing that obtrusive plugins do. What’s better, is you do not need to worry about the next update ruining your site layout, and that’s peace of mind.

What you need to do is open your functions.php file found in your theme’s folder. At this point, we will assume that you have some rudimentary knowledge of the PHP programming language. If you don’t there are plenty of good PHP tutorials you can look at online. If you are modifying a theme written by someone else you may need to use a child theme of that parent theme instead.

Add the following code at the bottom of the file and save. In the code where it says: return 20; change that number to anything your desire.

/* Custom Excerpt length */
function my_excerpt_length( $length ) {
    return 20;
add_filter( 'excerpt_length', 'my_excerpt_length', 999 );

There you have it. You now have complete control over WordPress’ default excerpt length. Now you do not have to worry about third-party plugins slowing down or compromising your website.


Official PHP functions documentation

WordPress developer reference

Found this article interesting? Follow Brightwhiz on Facebook, Twitter, and YouTube to read and watch more content we post.