Changing the excerpt length, wordpress 2.9

Changing the excerpt length in WordPress 2.9 isn’t hard at all. You’ll need to add some php to your functions.php to make this happen.

I’ve had to modify this to work in wordpress version 2.9 and have tested it up to wordpress 2.9.1

add the following to your functions.php right below the <?php at the top of the functions.php file

function my_wp_trim_excerpt($text) { // Fakes an excerpt if needed

if ( '' == $text ) {
$text = get_the_content('');
$text = apply_filters('the_content', $text);
$text = str_replace(']]&gt;', ']]&gt;', $text);
$text = strip_tags($text, '

');
$excerpt_length = 50;
$words = explode(' ', $text, $excerpt_length + 1);
if (count($words) &gt; $excerpt_length) {
array_pop($words);
array_push($words, ' ... <a href="'. get_permalink() . '">READ MORE</a>');
$text = implode(' ', $words);
}
}
return $text;
}
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'my_wp_trim_excerpt');

Change $excerpt_length = apply_filters(‘excerpt_length’, 35);

where 35 is the number of words you want in your excerpt

  • Trackback are closed
  • Comments (7)
    • Sergei Plaxienko
    • December 9th, 2009

    Does not work. At all.

  1. Works for me.. what version of WP do you use? Also paste a copy of your functions.php here and I’ll see if I can figure out whats up.. chances are you didn’t use the correct syntax.

    • CK
    • January 5th, 2010

    It does not work due to the fact that the code in quotes is rendered oddly and some of your symbols are messed. example: line 3 is suppose to have 2 single quotes, not a double quote (this goes for some another case in your code) and there is a > instead of a >. You’re also missing the permalink values in the array_push so it pulls the right link. The fix below worked for me, but I still can’t get rid of the original Read More generated by wordpress even though the script says it should remove it.

    Here’s the updated code with the corrections:

    function my_wp_trim_excerpt($text) { // Fakes an excerpt if needed
    $raw_excerpt = $text;
    if ( ” == $text ) {
    $text = get_the_content(”);

    $text = strip_shortcodes( $text );

    $text = apply_filters(‘the_content’, $text);
    $text = str_replace(‘]]>’, ‘]]>’, $text);
    $text = strip_tags($text);
    $excerpt_length = apply_filters(‘excerpt_length’, 55);
    $words = explode(‘ ‘, $text, $excerpt_length + 1);
    if (count($words) > $excerpt_length) {
    array_pop($words);
    array_push($words, ‘ … Read More …‘);
    $text = implode(‘ ‘, $words);
    }
    }
    return apply_filters(‘wp_trim_excerpt’, $text, $raw_excerpt);
    }

    remove_filter(‘get_the_excerpt’, ‘wp_trim_excerpt’);
    add_filter(‘get_the_excerpt’, ‘my_wp_trim_excerpt’);

    Anyone know the fix to remove the original excerpt? remove_filter() isn’t working in the functions.php.

    • CK
    • January 5th, 2010

    Darn rendering. Could the host edit this post so that the content is in stripped out code instead of html rendered copy? Thx

    • Kurt
    • February 14th, 2010

    @CK
    I have updated the code – thanks

    • Steven Fine
    • May 1st, 2010

    @Kurt

    Kurt – I want to get rid of the exerpts all together.

    I have tried everything without luck

    If I put the $excerpt_length = apply_filters(‘excerpt_length’, 0) will it work ?

    • me
    • May 4th, 2010

    In wordpress 2.9 skip all that other stuff and just do this:

    [code]]czozMzk6XCINCi8vIGNoYW5nZSBkZWZhdWx0IGxlbmd0aCBvZiB0aGVfZXhjZXJwdCBmcm9tIDU1DQpmdW5jdGlvbiBuZXdfZXhjZXJ7WyYqJl19cHRfbGVuZ3RoKCRsZW5ndGgpIHsNCglyZXR1cm4gMjA7DQp9DQphZGRfZmlsdGVyKFwnZXhjZXJwdF9sZW5ndGhcJywgXCduZXdfZXhjZXtbJiomXX1ycHRfbGVuZ3RoXCcpOw0KDQoNCi8vIGNoYW5nZSBleGNlcnB0IG1vcmUgWy4uLl0NCmZ1bmN0aW9uIG5ld19leGNlcnB0X21vcmUoJHtbJiomXX1wb3N0KSB7DQoJcmV0dXJuIFwnPGEgaHJlZj1cIlwnLiBnZXRfcGVybWFsaW5rKCkgLiBcJ1wiPi4uLm1vcmU8L2E+XCc7DQp9DQphZGRfZmlse1smKiZdfXRlcihcJ2V4Y2VycHRfbW9yZVwnLCBcJ25ld19leGNlcnB0X21vcmVcJyk7DQpcIjt7WyYqJl19[[/code]