Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have code in functions.php:

function string_limit_words($string, $word_limit)
{
  $words = explode(' ', $string, ($word_limit + 1));
  if(count($words) > $word_limit)
  array_pop($words);
  return implode(' ', $words);
}

but i need to limit excerpt in number of characters, could you help me with that?

share|improve this question

3 Answers 3

up vote 2 down vote accepted

i used this code in one of my last projects:

function ng_get_excerpt($count){
  $permalink = get_permalink($post->ID);
  $excerpt = get_the_content();
  $excerpt = strip_tags($excerpt);
  $excerpt = substr($excerpt, 0, $count);
  $excerpt = substr($excerpt, 0, strripos($excerpt, " "));
  $excerpt = rtrim($excerpt,",.;:- _!$&#");
  $excerpt = $excerpt.'<a href="'.$permalink.'" style="text-decoration: none;">&nbsp;(...)</a>';
  return $excerpt;
}

i got that from here:

http://wordpress.org/support/topic/limit-excerpt-length-by-characters

http://stackoverflow.com/questions/10923955/make-function-that-limits-text-not-show-last-punctuation-mark

it has the advantage of not allowing punctation on the end and ending with the last complete word

share|improve this answer
    
Very thanks for this advice, efficient one for me is just simple: "echo substr(get_the_excerpt(), 0,30);" taken from Your links. –  Marcin Oct 16 '12 at 21:46
    
@Marcin substr() will break. Never use it on UTF-8 encoded data. –  toscho Oct 16 '12 at 22:51

Use the function utf8_truncate() from this answer and fight your way through wp_trim_excerpt().

Sample code, not tested:

add_filter( 'excerpt_more', 'wpse_69436_excerpt_more' );

function wpse_69436_excerpt_more( $more )
{
    add_filter( 'wp_trim_excerpt', 'wpse_69436_trim_excerpt' );
    // we remove the more text here
    return '';
}

function wpse_69436_trim_excerpt( $excerpt )
{
    return utf8_truncate( $excerpt, 300 );
}
share|improve this answer

WordPress has a filter for that which is conveniently named excerpt_length and it accepts a number of chars so:

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

change 50 to whatever limit you want.

Update per @toscho comment:

that is the solution above is for words as well and not for chars so here is a quick one:

add_filter('the_excerpt','excerpt_char_limit');
function excerpt_char_limit($e){
    return substr($e,0,50);
}
share|improve this answer
1  
That’s not for characters, it is for words. –  toscho Oct 16 '12 at 19:02
    
@toscho My bad! updated with a simple solution –  Bainternet Oct 16 '12 at 19:12
    
How does it works? i put it as "echo custom_excerpt_length(); " but it doesn't work. –  Marcin Oct 16 '12 at 19:12
    
Thanks, now it works, but how to give some excerpts lengths for front page? –  Marcin Oct 16 '12 at 19:16
    
@Bainternet - I gave: "add_filter('the_excerpt2','excerpt_char_limit2'); function excerpt_char_limit2($e){ return substr($e,0,10); }" but it doesn't work. –  Marcin Oct 16 '12 at 19:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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