1

I know I can control the length of excerpts using a filter, however it sets the length globally. In my case I have different lengths of excerpts on three different pages.

This is what I have now. I'd like to improve it so it doesn't truncate words.

$content_to_excerpt = strip_tags( strip_shortcodes( get_the_content() ) );
$pos = strpos($content_to_excerpt, ' ', 160);
if ($pos !== false) {
 echo  "<p>" . substr($content_to_excerpt, 0, $pos) . "...</p>";
}

2 Answers 2

8

Set the filter dynamically based on the page you are loading. If category archive pages have a 100 word excerpt, but posts have a 10 word excerpt, and everything else uses the default:

function my_custom_excerpt_length( $orig ) {
    if( is_category() ) {
        return 100;
    } elseif( is_single() ) {
        return 10;
    }

    return $orig;
}
add_filter( 'excerpt_length', 'my_custom_excerpt_length' );

You can get even more specific, giving specific categories a different length.


In response to Rarst, a new template function the_excerpt_length() for outputting the excerpt with a specific number of words:

// Excerpt with a specific number of words, i.e. the_excerpt( 100 );
function the_excerpt_length( $words = null ) { 
    global $_the_excerpt_length_filter;

    if( isset($words) ) { 
        $_the_excerpt_length_filter = $words;
    }   

    add_filter( 'excerpt_length', '_the_excerpt_length_filter' );
    the_excerpt();
    remove_filter( 'excerpt_length', '_the_excerpt_length_filter' );

    // reset the global
    $_the_excerpt_length_filter = null;
}

function _the_excerpt_length_filter( $default ) { 
    global $_the_excerpt_length_filter;

    if( isset($_the_excerpt_length_filter) ) { 
        return $_the_excerpt_length_filter;
    }   

    return $default;
}

This would be used in your template file, i.e.:

<div class="entry">
    <?php the_excerpt_length( 25 ); ?>
</div>
4
  • One more thing that people often miss - you can have different excerpt length even on same page by adding and removing filter as necessary between calls.
    – Rarst
    Commented Feb 14, 2011 at 9:00
  • @Rarst Good call. Updated with a function.
    – user66
    Commented Feb 14, 2011 at 21:31
  • After adding the the latter example I get 500 error. I put both methods in functions.php and added add_filter( 'excerpt_length', 'the_excerpt_length' );
    – Eeyore
    Commented Feb 18, 2011 at 4:40
  • @Eeyore the_excerpt_length isn't a filter, it's a function that lets you echo an excerpt of a specific length. Sorry for the confusion. I'll add a usage example to the answer.
    – user66
    Commented Feb 18, 2011 at 18:32
1

I know I've seen this question on StackOverflow before. Not specific to WordPress excerpt, but on truncating strings on PHP without truncate words. Looking back on StackOverflow itself, I found this link pretty helpful on this subject.

The code being used there is:

// Original PHP code by Chirp Internet: www.chirp.com.au
// Please acknowledge use of this code by including this header. 
function myTruncate($string, $limit, $break=".", $pad="...") { 
  // return with no change if string is shorter than $limit
  if(strlen($string) <= $limit) return $string;

  // is $break present between $limit and the end of the string?
  if(false !== ($breakpoint = strpos($string, $break, $limit))) {
    if($breakpoint < strlen($string) - 1) {
      $string = substr($string, 0, $breakpoint) . $pad; 
    } 
  } 
  return $string; 
}

Hope this helps!

1
  • 2
    Never use strlen() on multibyte strings. mb_strlen() and mb_substr() are much better.
    – fuxia
    Commented Feb 14, 2011 at 17:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.