WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Limit excerpt length by characters

[Resolved] Limit excerpt length by characters

  • I’m using the following excerpt filter but it limits the post length by words. How can I put a limit on characters? Like Twitter. I can;t find any documentation in Codex.

    function new_excerpt_length($length) {
    	return 20;
    }
    add_filter('excerpt_length', 'new_excerpt_length');
Viewing 15 replies - 1 through 15 (of 25 total)
  • Nem,
    Looks like you need a custom function

    function get_the_twitter_excerpt(){
    $excerpt = get_the_content();
    $excerpt = strip_shortcodes($excerpt);
    $excerpt = strip_tags($excerpt);
    $the_str = substr($excerpt, 0, 20);
    return $the_str;
    }

    you should be able to put the above into your functions and this

    <?php echo 'Excerpt: '.get_the_twitter_excerpt(); ?>

    into your loop.

    Brilliant! This works very nicely. Thanks jakep!

    Mate I’ve found a simpler method in an old theme I made.

    <?php echo substr(get_the_excerpt(), 0,30); ?>

    It does the same thing.

    boom!

    php has so many ways..

    this is great but do you know how I would end with a full word?

    Add this to the function –

    $the_str = substr($excerpt, 0, 20);
    $the_str = trim(preg_replace( '/\s+/', ' ', $the_str));
    return $the_str;

    boom!

    Even better –

    function get_the_popular_excerpt(){
    $excerpt = get_the_content();
    $excerpt = preg_replace(" (\[.*?\])",'',$excerpt);
    $excerpt = strip_shortcodes($excerpt);
    $excerpt = strip_tags($excerpt);
    $excerpt = substr($excerpt, 0, 40);
    $excerpt = substr($excerpt, 0, strripos($excerpt, " "));
    $excerpt = trim(preg_replace( '/\s+/', ' ', $excerpt));
    $excerpt = $excerpt.'... <a href="'.$permalink.'">more</a>';
    return $excerpt;
    }

    umm. this saved me much. thanks! 🙂 I be happy now.

    @seanjacob

    Can you explain how to use your function ?

    Thanks,
    Anthony

    In functions.php add

    function get_excerpt(){
    $excerpt = get_the_content();
    $excerpt = preg_replace(" (\[.*?\])",'',$excerpt);
    $excerpt = strip_shortcodes($excerpt);
    $excerpt = strip_tags($excerpt);
    $excerpt = substr($excerpt, 0, 40);
    $excerpt = substr($excerpt, 0, strripos($excerpt, " "));
    $excerpt = trim(preg_replace( '/\s+/', ' ', $excerpt));
    $excerpt = $excerpt.'... <a href="'.$permalink.'">more</a>';
    return $excerpt;
    }

    Change the value 40 to how many characters you want to display. And also ‘more’ to what text you want to display.

    Then call your function in index.php or where ever your planning on getting your posts.

    <?php echo get_excerpt(); ?>

    Thank you for your answer.

    I’ve tried this : http://bavotasan.com/tutorials/limiting-the-number-of-words-in-your-excerpt-or-content-in-wordpress/

    and it did the job, I can ajust the excerpt length how I want with
    <?php echo excerpt(25); ?>

    And the last word of the excerpt isn’t truncated 🙂

    Glad you found a solution. My code doesn’t truncate the last word either and with my problem I needed it to be by characters not words.

    seanjacob – this is very useful, thank you.

    Can you tell me how i auto-wrap the excerpt in <p></p> tags? Like the_excerpt() does?

    $excerpt = $excerpt.'... <a href="'.$permalink.'">more</a>';

    Replace this line in my code with –

    $excerpt = '<p>'.$excerpt.'... <a href="'.$permalink.'">more</a></p>';

Viewing 15 replies - 1 through 15 (of 25 total)
  • The topic ‘[Resolved] Limit excerpt length by characters’ is closed to new replies.