• ambius

    (@ambius)


    I’m using the_excerpt() inside my loop on index.php

    On posts without a defined excerpt, it should return the first 55 words of the content, and sometimes it does, but other times it will return as many as 68 words. I tried setting the limit to 20 words. This made the excerpts shorter but still not exactly 20 words, they range from 18-30.

    is this a known bug in wordpress? is there a known way to get it to properly count words? I don’t want to write a custom excerpt for every post I make, but I do want the lengths to be more consistent.

Viewing 4 replies - 1 through 4 (of 4 total)
  • MichaelH

    (@michaelh)

    Hmm–are specifing the number of words for the excerpt with a filter or plugin. If so, might provide that code and maybe someone can spot the problem.

    Related:
    Excerpt
    Template_Tags/the_excerpt
    http://wordpress.org/extend/plugins/advanced-excerpt/
    http://wordpress.org/extend/plugins/excerpt-editor/

    Thread Starter ambius

    (@ambius)

    I’m not using any plugins that affect post content or excerpts.

    this is the very simple code I’m using

    <? query_posts('posts_per_page=10&caller_get_posts=1');
    function new_excerpt_length($length) {
    	return 20;
    }
    add_filter('excerpt_length', 'new_excerpt_length');
    if( have_posts()) : while (have_posts()) : the_post(); ?>
        <div class="post">
            <div class="post-title"><a>" rel="bookmark" title="Permanent Link to <? the_title_attribute(); ?>"><? the_title(); ?></a></div>
            <div class="post-date"><? the_date('F j, y') ?></div>
            <div class="post-content"><? the_excerpt() ?></div>
            <div class="post-footer">POSTED BY <? the_author() ?> and last updated on <? the_modified_time('F jS, Y'); ?> at <? the_modified_time('g:i a'); ?>
                <? comments_popup_link('« 0 Comments »', '1 Comment', '% Comments'); ?>
            </div>
        </div>
        <p>&nbsp;</p>
    <? endwhile; endif; //end the loop ?>

    and here is the page on my test server if you want to see the wordcount outputs for yourself
    http://uofmbooks.com/wordpress/
    the first 7 posts are fine, but the 8th post “Minutes for 2-1-10” contains 30 words. Minutes for 10-27 has 22 words. etc…

    MichaelH

    (@michaelh)

    Here’s the 2.9 core function that does that:

    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 = explode(' ', $text, $excerpt_length + 1);
    		if (count($words) > $excerpt_length) {
    			array_pop($words);
    			$text = implode(' ', $words);
    			$text = $text . $excerpt_more;
    		}
    	}
    	return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
    }

    I have noticed this too and after some experimenting I think I have found the cause.

    If you have extra spaces between words those spaces will get counted as words. It’s easy to overlook as when editing using the visual editor the hidden spaces are not shown, you have to go into the html editor to see them.

    Clearly it’s a wordpress bug as it should ignore the extra spaces, but at least there is a work around, albeit a rather tedious post editing one.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘the_excerpt() returns wrong number of words’ is closed to new replies.