WordPress.org

Ready to get started?Download WordPress

Forums

Can't change excerpt length (TwentyTen child theme) (5 posts)

  1. dougalg
    Member
    Posted 3 years ago #

    Hi, I am making a child of the twenty-ten theme, and would like to have slightly longer excerpts. I modified the loop so that it displays the_excerpt() by default, and only uses the auto-generated excerpts.

    However, I can't change the length to be more than the word-press default. I tried editing my funcitons.php file with the following:

    add_action( 'after_setup_theme', 'my_child_theme_setup' );
    function my_child_theme_setup() {
        // We are providing our own filter for excerpt_length (or using the unfiltered value)
        remove_filter( 'excerpt_length', 'twentyten_excerpt_length' );
        function repo_excerpt_length( $length ) {
            return 200;
        }
        add_filter( 'excerpt_length', 'repo_excerpt_length', 200 );
    }

    But it doesn't work. I also tried editing TwentyTen's functions.php file and that doesn't work either. This also seems to apply to the "read more" link. TwentyTen's custom read more link function does not actually affect the look of the site either.

    Thanks for any help!

  2. Nalin
    Member
    Posted 3 years ago #

    try this:

    open the file called formatting.php which located wp-include folder. then find following code.

    function wp_trim_excerpt($text) {
    	$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);
    		$excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
    		$words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
    		if ( count($words) > $excerpt_length ) {
    			array_pop($words);
    			$text = implode(' ', $words);
    			$text = $text . $excerpt_more;
    		} else {
    			$text = implode(' ', $words);
    		}
    	}
    	return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
    }

    now change the limit as you preference

    $excerpt_length = apply_filters('excerpt_length', 55);

  3. dougalg
    Member
    Posted 3 years ago #

    So that didn't work, but now I know why. When I first installed I apparently installed the advanced excerpt plugin and forgot about it... It override all the other code.

    Thanks so much for you time.

  4. smoo
    Member
    Posted 3 years ago #

    Thank you dougalg;
    I'm only recently getting to grips with filters in my child themes and have been trying to change the length of my excerpts for many hours now, using a variety of combinations of code;
    I hadn't used
    add_action( 'after_setup_theme', 'my_child_theme_setup' );
    before however, and it works a treat!

    I was beginning to think the_excerpt was bullet proof..
    Thanks again :o)

  5. swinggraphics
    Member
    Posted 2 years ago #

    Because this discussion kept coming up in my searches to do a basic change of the excerpt length, I'll highlight the specific code to add to a theme's functions.php file:

    function custom_excerpt_length( $length ) {
    	return 20;
    }
    add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

    Note the third argument in the call to add_filter(). It doesn't work without it. I updated the code snippet I found on the the_excerpt in the Codex that didn't have it, and therefore frustrated me almost no-end.

Topic Closed

This topic has been closed to new replies.

About this Topic