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 (10)
    • Sergei Plaxienko
    • December 9th, 2009

    Does not work. At all.

    • 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:


    // change default length of the_excerpt from 55
    function new_excerpt_length($length) {
    return 20;
    }
    add_filter('excerpt_length', 'new_excerpt_length');

    // change excerpt more [...]
    function new_excerpt_more($post) {
    return '...more';
    }
    add_filter('excerpt_more', 'new_excerpt_more');

  1. It’s excellent site, I was looking for something like this

    • Fred
    • March 1st, 2011

    Hello i have a problem with this function.
    The texte of my link is appear in top of my single page…
    Can you help me please?

    This is my function :

    ‘, ‘]]>’, $text);
    $text = strip_tags($text);
    $excerpt_length = 42; // Changes excerpt character count
    $words = explode(‘ ‘, $text, $excerpt_length + 1);
    if (count($words) > $excerpt_length) {
    array_pop($words);
    array_push($words, ‘Read Full Post →‘); // Changes link text
    $text = implode(‘ ‘, $words);
    }
    }
    return $text;
    }
    ?>

    Thinks a lot.
    PS : this is the page where you can see the probleme
    http://actu.loiregrafix.fr/un-site-vitrine-pour-la-fonderie-plomb-navy-lest

      • Kurt Turner
      • March 24th, 2011

      Try moving the code around within your .php.. also wrap it in a DIV and format as necessary.