Custom Excerpt Length in WordPress

Posted by | October 14, 2013 | Tutorials & Tips | 4 Comments

When designing custom themes or editing existing themes in WordPress, sometimes the native excerpt length isn’t quite suited to the design and layout.  This is actually a really simple fix for anyone needing something that isn’t too hacky and wants ultimate control over the length of the excerpt.  All you have to do is first, paste the following code into your functions.php file:

Once this is done, you can just use the following code in any of your template files, replacing the number 25 with whatever number you like.  See, simples!  You can also use this with the content as well as the excerpt, all you have to do is change the word ‘excerpt’ below to ‘content’.

Let me know in the comments below what you think of this method, and if you have a better way of doing this then please post your suggestions below too!

  • Bill Gilmore

    Pretty sweet, easier than the last method I found

    • http://www.kristam-moffett.com/ Kristam Moffett

      Yeh it’s pretty nice, plus you can just take lines 2-12 if you only want to option of shortening the excerpt, likewise lines 14-26 if you only want the content.

  • Lance

    Amazing bit of code. I also took the excerpt function, replaced $excerpt with $title, and changed get_the_excerpt to get_the_title. Works perfectly to create a custom title length. Thank you again.

    • http://www.kristam-moffett.com/ Kristam Moffett

      Good call Lance. This can be used with almost any WordPress function that outputs a string – I’ve used it myself to create some shorter titles in the past and it works like a charm.

Have a project you would like to chat about? Get In Touch!