• Home
  • Systems
  • Networking
  • WordPress
  • Web
  • Geekery

Analysis and Review

Changing the excerpt length, wordpress 2.9

August 4, 2009 by Kurt Turner

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
[php]
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(‘]]>’, ‘]]>’, $text);
$text = strip_tags($text, ‘

‘);
$excerpt_length = 50;
$words = explode(‘ ‘, $text, $excerpt_length + 1);
if (count($words) > $excerpt_length) {
array_pop($words);
array_push($words, ‘ … READ MORE‘);
$text = implode(‘ ‘, $words);
}
}
return $text;
}
remove_filter(‘get_the_excerpt’, ‘wp_trim_excerpt’);
add_filter(‘get_the_excerpt’, ‘my_wp_trim_excerpt’);[/php]
Change $excerpt_length = apply_filters(‘excerpt_length’, 35);

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

Filed Under: Wordpress Tagged With: excerpt lenth, Wordpress

Comments

  1. Sergei Plaxienko says

    December 9, 2009 at 7:53 am

    Does not work. At all.

  2. Kilroy says

    December 9, 2009 at 4:17 pm

    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.

  3. CK says

    January 5, 2010 at 11:08 am

    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.

  4. CK says

    January 5, 2010 at 11:10 am

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

  5. Kurt says

    February 14, 2010 at 9:39 pm

    @CK
    I have updated the code – thanks

  6. Steven Fine says

    May 1, 2010 at 8:29 pm

    @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 ?

  7. me says

    May 4, 2010 at 11:22 am

    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');

  8. Daryl Łomża says

    January 4, 2011 at 6:01 pm

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

  9. Fred says

    March 1, 2011 at 8:42 pm

    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

  10. Kurt Turner says

    March 24, 2011 at 6:49 pm

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

Latest and Greatest

Apache vs Nginx for WordPress

Certbot Lets Encrypt And WordPress MU Multi-Site

How To Find and Replace Multiple File in MS Word

How To Get Stretch Res on Windows 10 AMD Radeon For Game Play

This is the ASUS Chromebox we've been running for 2 years as our "brain" for our home grown digital display kiosk.

DIY Digital Signage Bulletin Board or Kiosk

© Copyright Analysis And Review · All Rights Reserved ·