The MU forums have moved to WordPress.org

Excerpt shortening (4 posts)

  1. dixyantar
    Member
    Posted 13 years ago #

    I use <?php the_excerpt();?> in my blog homepage.
    What I want is that instead of displaying the teaser i.e first 55 words of my post it should display say 20 words.

    Is there a way to do this or some other template tag to make this possible.

  2. andrea_r
    Moderator
    Posted 13 years ago #

    you can manually fill in the exact excerpt you want in the excerpt field under the post. Otherwise, it uses the default.

  3. Klark0
    Member
    Posted 13 years ago #

    http://chrismeller.com/changing-the-length-of-the_excerpt-in-wordpress

    Put in your home theme's function.php file or in a .php file of its own in mu-plugins.

    I do something similar for my tag pages and whatnot.

  4. dixyantar
    Member
    Posted 13 years ago #

    Hey Klarko, thanks. God knows why but this does not work for me.
    I am on 2.6 and tried it in functions.php as well as mu-plugins. Doesn't work man .

    Here's my code.

    <?php
    
    remove_filter(‘get_the_excerpt’, ‘wp_trim_excerpt’);
    
    function new_excerpt($text) { // Fakes an excerpt if needed
    	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 = 25;
    		$words = explode(' ', $text, $excerpt_length + 1);
    		if (count($words) > $excerpt_length) {
    			array_pop($words);
    			array_push($words, '[...]');
    			$text = implode(' ', $words);
    		}
    	}
    	return $text;
    }
    
    add_filter(‘get_the_excerpt’, ‘new_excerpt’);
    ?>

About this Topic

  • Started 13 years ago by dixyantar
  • Latest reply from dixyantar