How to change the length of the_excerpt() in wordpress
Filed in Tutorials & Resources
Time spent as a freelance web designer often means i come across problems – this particular one was driving me crazy for a while! – Often is the case, you’re in WordPress you’ve created your index page, your featured post content slider and your page is coming together… but the WordPress post excerpt is either too long or too short. Maybe you need to change the length of the_excerpt to better fit in with your design or maybe you just want more juicy text in the page for Google to chew.
For those who haven’t dealt with excerpts in WordPress before, it’s basically used to display the first few lines or words of your WordPress post – allowing you to provide an insight into the post to encourage users to read more.
Changing the_excerpt length in WordPress is incredibly simple, just follow these steps:
Open up your functions.php template file from your theme directory and find the closing “?>” at the end of your functions file. Just above it, paste the following code:
1 2 3 | add_filter('excerpt_length', 'my_excerpt_length'); function my_excerpt_length($length) { return <strong>40</strong>; } |
The WordPress excerpt is by default set at 55 words, the code above will lower that to 40 words, you can put whatever number you like in here depending on your requirements.
Once you’ve added the code to your functions file and set the number of words you want to display, add the excerpt to the post like you would normally using
1 | the_excerpt(); |
.
And that’s it :-).
Based in London, Working Everywhere
I’m a Freelance Web Designer and WordPress developer, I’m based in London but work with client’s in the UK and all over the world.