excerpt in characters

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 = mb_substr( $excerpt, 0, $count );
  $excerpt = mb_substr( $excerpt, 0, strripos( $excerpt, " " ) );
  $excerpt = rtrim( $excerpt, ",.;:- _!$&#" );
  $excerpt = $excerpt . '<a href="'.$permalink.'" style="text-decoration: none;">&nbsp;(...)</a>';
  return $excerpt;
}

I got it from here:

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

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

It has the advantage of not allowing punctuation on the end and ending with the last complete word

Using the filters as suggested by @medhamza7 or @bainternet or @fuxia is preferable.

Leave a Comment