How can I change the length of the_excerpt() in Wordpress without editing core files?
Well, first we need to figure out exactly where the excerpt text is truncated. Checking wp-includes/default-filters.php, around line 128 we see:
Ok, so that’s how it’s cut off. The first step in our fix is to prevent the default function from running and truncating the text at the default length. The easiest way to do this? Simply remove the filter by adding a line to your theme’s functions.php file:
Great. So now we’ve got the_excerpt() looking exactly like the_content(). Now we need to create our own pretty little function to handle truncating the_excerpt to the length we need. Since those crazy Wordpress devs have already thought of everything, we’ll use their original function as a template for our new one. The original function can be found in wp-includes/formatting.php, around line 779:
global $post;
if ( ” == $text ) {
$text = get_the_content(”);
$text = apply_filters(‘the_content’, $text);
$text = str_replace(‘]]>’, ‘]]>’, $text);
$text = strip_tags($text);
$excerpt_length = 55;
$words = explode(‘ ‘, $text, $excerpt_length + 1);
if (count($words) > $excerpt_length) {
array_pop($words);
array_push($words, ‘[...]‘);
$text = implode(‘ ‘, $words);
}
}
return $text;
}
See the line $excerpt_length = 55;? That’s the one we want to change!
Take your new wp_trim_excerpt function and stick it in your theme’s functions.php file. Be sure to rename it to something unique. You should end up with something similar to this:
global $post;
if ( ” == $text ) {
$text = get_the_content(”);
$text = apply_filters(‘the_content’, $text);
$text = str_replace(‘]]>’, ‘]]>’, $text);
$text = strip_tags($text);
$excerpt_length = 75;
$words = explode(‘ ‘, $text, $excerpt_length + 1);
if (count($words) > $excerpt_length) {
array_pop($words);
array_push($words, ‘[...]‘);
$text = implode(‘ ‘, $words);
}
}
return $text;
}
Note that I only made two changes: 1) renamed the function to “custom_trim_excerpt”, and 2) changed the excerpt_length to 75.
Great, we’re almost there! One step left. Now we have to tell Wordpress to use our new custom function to truncate the_excerpt. We’ll do this by adding a new filter, similar to the one we removed at the very beginning. Stick this line in your theme’s functions.php file:
Be sure to substitute your new function’s name, assuming you didn’t use the same one I did, and you should be all set!
Note: This guide was written using Wordpress 2.2. The exact procedure may vary with versions, but should be fairly similar.
Worst. Organized. Project. Evar.
For the last two weeks, I’ve been working to design a one-off enrollment system1 for a client, and it seems like it’s just been one road-block after another.
First, their data was incorrect. And then again. And for good measure, a third time. To be fair, I don’t know if it was their fault or ours the second and third time (mis-communication somewhere about what translations we were making), but the first time certainly was2.
Then, there’s the fact that when I notified the person in charge of coordinating this data exchange with the client, they apparently “missed” the email… And so three days later I’m asking them where the hell my answers are, and they have no clue what I’m talking about.
Next, apparently no one really knows what data the client wants to get back from us for entry into their system. On some things, they claim to want all coverages. For others, just changes. Then there’s this one piece of information they don’t seem to have thought about nor want at all…
Finally, 4 days before the system is to go live and we’ll have thousands of people from across the country calling in to enroll using it, as I’m working my ass off to correct a few bugs and get final testing in, I notice that the spreadsheet I originally got says that the client is expecting data back for a coverage we’re not supposed to be enrolling them.
Uhh, what? How can we give them back data on something we’re not enrolling? Back to the project manager we go… “Oh, right… That spreadsheet is old, I’ll see if I can find a new version for you.”
WTF? 4 days from live and I’m the one that has to realize no one bothered to give the programmer the real specs? If these were freelance clients, I’d never work for them again… Alas, they’re internal company employees and I’m not their manager…
Tags:
Post Meta
Atom