Custom Excerpt Lengths in WordPress

Note: This will throw an error if you are using PHP 5.2 or lower. 5.3 and above will be fine. Thanks to FoxyPaco for the info.

WordPress generally handles all of your content pretty nicely, including the excerpt, but sometimes you need a custom excerpt length and WordPress doesn’t support that out of the box. After some looking around on the internet I came across two solutions. First, an out-of-date plugin that wasn’t going to get the job done and a custom piece of code that goes into functions.php.

I went with the latter and chucked this block of code into my functions.php file.

function custom_excerpt($new_length = 20, $new_more = '...') {

  // use the variable passed from $new_length as the length of the excerpt
  add_filter('excerpt_length', function () use ($new_length) {
    return $new_length;
  }, 999);

  // determine what comes at the end of the excerpt (in this case ...)
  add_filter('excerpt_more', function () use ($new_more) {
    return $new_more;
  });

  // generate the current excerpt
  $output = get_the_excerpt();

  // use wptexturize to basically sanitize the excerpt
  $output = apply_filters('wptexturize', $output);

  // convert_chars to remove metadata tags and convert others to unicode 
  $output = apply_filters('convert_chars', $output);

  // wrap it back up in p tags
  $output = '<p>' . $output . '</p>';
  // the above line may not be needed depending on the status of wpautop

  // echo that sucker
  echo $output;
}

So that looks long, but if you take the comments out, it’s much shorter. It gives you complete control over your excerpts and all you have to do is switch from using

<?php the_excerpt(); ?>

to

<?php custom_excerpt(); ?>

After you use the new function you can place any number inside the () and that number will then be passed as the new $new_length variable discussed in the code above.

Note: if you try to use the_excerpt(); later in the template, it will default to the closest custom_excerpt() number. Thanks @pbaylies for pointing this out.

That’s all you have to do for a shiny custom excerpt length, thanks to LordAzriel on the support forums. The thing I like the most about this custom excerpt length is that you can use it wherever you want and just place a new number inside the function. No more going back to functions.php every time you want a new length. It’s easy, simple and the options for custom excerpt lengths become as endless as you could want. It’s all just a new number inside ().

If you have a better way to accomplish this, let me know. I’d be happy to hear about it.

convert_chars in the Codex
wptexturize in the Codex

Note: The forum topic says it will only work if you are running PHP 5.3 or higher, which you hopefully are.

Subscribe
Notify of
guest
8 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
trackback

[…] That’s it. We’ve broken down the full array and now understand a little more about how to remove div and ul from wp_nav_menu. Like I said, if you have more insight, please feel free to share. Otherwise, check out some more WordPress functions: Custom Taxonomy Category Dropdowns or Custom Excerpt Lengths. […]

Scott Dixon
Scott Dixon
8 years ago

Thanks for this. The method I was using was displaying image captions in the excerpt, this way seems better.

Alex Franco
Alex Franco
7 years ago

Thank you for the code. However, this is causing a PHP Parse error: syntax error, unexpected T_FUNCTION on my live site. It’s not causing any errors for my local install using MAMP and my site and theme remains functioning. But using this code breaks my theme completely on my live site. The specific code is from line 4 in your pasted code. Have any ideas what my be causing this?

johnbhartley
johnbhartley
7 years ago
Reply to  Alex Franco

Do you have debug turned on for your local install and if so, anything in there that you see? I know you said you’re getting the error on the live site, just trying to figure out if there are any differences between local and live.

Alex Franco
Alex Franco
7 years ago
Reply to  johnbhartley

I have PHP error log turned on now for MAMP. Here is what it says:

[04-Sep-2013 21:57:53 UTC] PHP Fatal error: Call to undefined function custom_excerpt() in /Library/WebServer/WebDocuments/steelvalleyrugby/wp-content/themes/steelvalleyrugby/my-templates/home-slider.php on line 119

Here is a link to my pastebin for the home-slider.php portion with the error. The referencing error from log will be on line 19 on pastebin. http://pastebin.com/5VB8pL1H

And here’s a link to my functions portion for the excerpt. http://pastebin.com/w84JE688

I included screen shots too. This is what happens now with local machine. I deleted the “wrap it up in p tags portion” because it was doing this. http://cl.ly/image/232P402h2y1s

When I remove it, it keeps my styles but now the Read More link isn’t working. http://cl.ly/image/2P2o3a2R0d1k

When I use the_excerpt call in other loops, my Read More link works fine. Thanks again for your help.

johnbhartley
johnbhartley
7 years ago
Reply to  Alex Franco

Thanks for the follow up. I think there must have been something in WP 3.6 that made the above code funky. I’ll re-test in the next week or so and see if I can find a solution.

FoxyPaco
FoxyPaco
5 years ago
Reply to  Alex Franco

I know this is two years late but if no one’s figured out yet why this error happens, its because this custom excerpt function requires PHP 5.3 or higher. If you are on PHP 5.2 or lower you will get this error.

johnbhartley
johnbhartley
5 years ago
Reply to  FoxyPaco

Thanks for the extra info. At this point most folks have hopefully moved to 5.3, but I’ll add a note for those that are still on < 5.3

John Hartley is a Director of Product Engineering at Beam Dental in Columbus, OH. With 5+ years of leadership experience he has worked in startups, agencies, and began his career as a freelance Front End Developer. Always looking to iterate, this blog is a place for him to share his knowledge as well as hone his craft, challenge assumptions, and build a strong base of leadership and management knowledge. Connect with him on LinkedIn

8
0
Would love your thoughts, please comment.x
()
x