excerpt in characters

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.'" rel="nofollow noreferrer noopener" 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.

Method 2

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 );
}

Method 3

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);
}

Method 4

For a better way, you can use the get_the_excerpt filter:

function get_excerpt($excerpt="",$limit=140){

    $excerpt = preg_replace(" ([.*?])",'',$excerpt);
    $excerpt = strip_shortcodes($excerpt);
    $excerpt = strip_tags($excerpt);
    $excerpt = mb_substr($excerpt, 0, $limit);
    $excerpt = mb_substr($excerpt, 0, strripos($excerpt, " "));
    $excerpt = trim(preg_replace( '/s+/', ' ', $excerpt));
    $excerpt = $excerpt.'...';
    return $excerpt;
}   
add_filter('get_the_excerpt',"get_excerpt");

Change the $limit=140 to the number of characters you want. Also if you want in different way:

add_filter('get_the_excerpt',function ($excerpt="",$limit=140){

    $excerpt = preg_replace(" ([.*?])",'',$excerpt);
    $excerpt = strip_shortcodes($excerpt);
    $excerpt = strip_tags($excerpt);
    $excerpt = mb_substr($excerpt, 0, $limit);
    $excerpt = mb_substr($excerpt, 0, strripos($excerpt, " "));
    $excerpt = trim(preg_replace( '/s+/', ' ', $excerpt));
    $excerpt = $excerpt.'...';
    return $excerpt;
});

That will make avoid any conflict like existing name of function get_excerpt.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x