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.

What I'm trying to do:
I want to display the_excerpt, but I have a maximum of x characters the_excerpt may use, but I don't want to display a couple of characters of a word, only whole words.

Some more information:
This code is on a non-single/non-permalink web page such as archives, categories, front page, and searches, that makes me unable to use <!--more-->.

The code:
This is the code I use:

add_filter( 'excerpt_length', function( ) {
    return 20;
} );

if ( have_posts() ):
    while( have_posts() ): the_post();
        the_excerpt( );
    endwhile;
endif;
share|improve this question
    
possible duplicate of limit the words in the post content and add read more link –  kaiser Mar 15 '13 at 11:39
    
Try to implement this solution in the filter: stackoverflow.com/questions/1233290/… –  Muthuswamy Mar 15 '13 at 12:11
add comment

2 Answers

add_filter('wp_trim_excerpt', function($text){    
   $max_length = 140;

   if(mb_strlen($text, 'UTF-8') > $max_length){
     $split_pos = mb_strpos(wordwrap($text, $max_length), "\n", 0, 'UTF-8');
     $text = mb_substr($text, 0, $split_pos, 'UTF-8');
   }

   return $text;
});

This should take into account your max length and split the text at the nearest word boundary. Apply the filter, and call the_excerpt(); in your templates


Apparently there's a wp_trim_words function from WP 3.3 that you can also use, but from the source looks very inefficient. Appart from using 3 regexes, it splits the text into an array of words, and this can get very slow and memory exhaustive for large chunks of text...

share|improve this answer
add comment

After some puzzling, I found this solution:

$limit_characters = 120;

function new_excerpt_length( ) {
    global $length;
    return $length;
}

function get_the_excerpt_special( ) {
    global $limit_characters, $length;

    if(strlen( get_the_excerpt() ) > $limit_characters ) {
        $length--;
        return get_the_excerpt_special();
    } else {
        the_excerpt();
    }
}

if ( have_posts() ):
    while( have_posts() ): the_post();
        add_filter( 'excerpt_length', 'new_excerpt_length' );

        $length = 20;
        get_the_excerpt_special( );

        remove_filter( 'excerpt_length', 'new_excerpt_length' );

    endwhile;
endif;

It works! YAY! But since it has to repeatably call get_the_excerpt, the page slows down a bit.

share|improve this answer
2  
This cannot work. Your code will break multibyte characters. See this answer for proper truncating. –  toscho Mar 15 '13 at 11:38
1  
Another answer that limits the amount of words. –  kaiser Mar 15 '13 at 11:40
1  
@toscho thanks! Much quicker than what I had :-) –  Mike Madern Mar 15 '13 at 11:45
1  
@kaiser thanks! Also much quicker :-) –  Mike Madern Mar 15 '13 at 11:47
add comment

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.