Forums

[resolved] Limit excerpt length by characters (26 posts)

  1. nemci7v
    Member
    Posted 1 year ago #

    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');
  2. jakep_sf
    Member
    Posted 1 year ago #

    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.

  3. nemci7v
    Member
    Posted 1 year ago #

    Brilliant! This works very nicely. Thanks jakep!

  4. nemci7v
    Member
    Posted 1 year ago #

    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.

  5. jakep_sf
    Member
    Posted 1 year ago #

    boom!

  6. nemci7v
    Member
    Posted 1 year ago #

    php has so many ways..

  7. seanjacob
    Member
    Posted 1 year ago #

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

  8. seanjacob
    Member
    Posted 1 year ago #

    Add this to the function -

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

    boom!

  9. seanjacob
    Member
    Posted 1 year ago #

    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;
    }
  10. normanbird
    Member
    Posted 11 months ago #

    umm. this saved me much. thanks! :-) I be happy now.

  11. anthonymasure
    Member
    Posted 10 months ago #

    @seanjacob

    Can you explain how to use your function ?

    Thanks,
    Anthony

  12. seanjacob
    Member
    Posted 10 months ago #

    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(); ?>

  13. anthonymasure
    Member
    Posted 10 months ago #

    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 :)

  14. seanjacob
    Member
    Posted 10 months ago #

    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.

  15. trixienolix
    Member
    Posted 10 months ago #

    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?

  16. seanjacob
    Member
    Posted 10 months ago #

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

    Replace this line in my code with -

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

  17. keesiemeijer
    moderator
    Posted 10 months ago #

    Or use this to return the excerpt in a paragraph:
    return apply_filters('the_content', $excerpt);

  18. bobjones
    Member
    Posted 9 months ago #

    seanjacob, no matter what number I set, your script will not display more than 64 characters.

  19. marcosmyara
    Member
    Posted 6 months ago #

    It seems like ".$permalink." is calling the actual url of the page where i'm at, and not the page of the single post.

    Is it only me? When i click "more" the page just reloads.

  20. seanjacob
    Member
    Posted 6 months ago #

    $permalink = get_permalink($post->ID);

    Try adding this to the top of the function.

  21. seanjacob
    Member
    Posted 6 months ago #

    Use this function if you're planning on using it more than once with a different amount of characters.

    function 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 = $excerpt.'... <a href="'.$permalink.'">more</a>';
      return $excerpt;
    }

    Call the function plus the amount of characters -

    <?php echo get_excerpt(125); ?>

  22. marcosmyara
    Member
    Posted 6 months ago #

    cheers!!

  23. mowse
    Member
    Posted 6 months ago #

    Been trying to figure this out: Is there a way to use this code, creating a character limit, without truncating the last word? (I would still want the read more ellipses.)

    Any help would be appreciated!

  24. seanjacob
    Member
    Posted 6 months ago #

    Without the truncating -

    function get_excerpt($count){
      $permalink = get_permalink($post->ID);
      $excerpt = get_the_content();
      $excerpt = strip_tags($excerpt);
      $excerpt = substr($excerpt, 0, $count);
      $excerpt = $excerpt.'... <a href="'.$permalink.'">more</a>';
      return $excerpt;
    }
  25. mowse
    Member
    Posted 6 months ago #

    Thank you, seanjacob!...I'll give it a shot later today! :)

  26. seanjacob
    Member
    Posted 6 months ago #

    Haha nice one! No problem.

Topic Closed

This topic has been closed to new replies.

About this Topic