How to set character limit on the_content() and the_excerpt() in wordpress

How do I set a character limit on the_content() and the_excerpt() in wordpress? I have only found solutions for the word limit – I want to be able to set an exact amount characters of outputted.

Related posts

Leave a Reply

10 comments

  1. Or even easier and without the need to create a filter: use PHP’s mb_strimwidth to truncate a string to a certain width (length). Just make sure you use one of the get_ syntaxes.
    For example with the content:

    <?php $content = get_the_content(); echo mb_strimwidth($content, 0, 400, '...');?>

    Update 2022

    mb_strimwidth breaks the HTML if the comment tag is used. use official wordpress wp_trim_words functions

    <?php $content = get_the_content(); echo wp_trim_words( get_the_content(), 400, '...' );?>

    This will cut the string at 400 characters and close it with ....
    Just add a “read more”-link to the end by pointing to the permalink with get_permalink().

    <a href="<?php the_permalink() ?>">Read more </a>
    

    Of course you could also build the read more in the first line. Than just replace '...' with '<a href="' . get_permalink() . '">[Read more]</a>'

  2. You could use a WordPress filter callback function. In your theme’s directory, locate or create a file called functions.php and add the following in:

    <?php   
      add_filter("the_content", "plugin_myContentFilter");
    
      function plugin_myContentFilter($content)
      {
        // Take the existing content and return a subset of it
        return substr($content, 0, 300);
      }
    ?>
    

    The plugin_myContentFilter() is a function you provide that will be called each time you request the content of a post type like posts/pages via the_content(). It provides you with the content as an input, and will use whatever you return from the function for subsequent output or other filter functions.

    You can also use add_filter() for other functions like the_excerpt() to provide a callback function whenever the excerpt is requested.

    See the WordPress filter reference docs for more details.

  3. This also balances HTML tags so that they won’t be left open and doesn’t break words.

    add_filter("the_content", "break_text");
    function break_text($text){
        $length = 500;
        if(strlen($text)<$length+10) return $text;//don't cut if too short
    
        $break_pos = strpos($text, ' ', $length);//find next space after desired length
        $visible = substr($text, 0, $break_pos);
        return balanceTags($visible) . " […]";
    } 
    
  4. For Using the_content() functions (for displaying the main content of the page)

    $content = get_the_content();
    
    echo substr($content, 0, 100);
    

    For Using the_excerpt() functions (for displaying the excerpt-short content of the page)

    $excerpt= get_the_excerpt();
    
    echo substr($excerpt, 0, 100);
    
  5. wp_trim_words() This function trims text to a certain number of words and returns the trimmed text.

    $excerpt = wp_trim_words( get_the_content(), 40, '<a href="'.get_the_permalink().'">More Link</a>');
    

    Get truncated string with specified width using mb_strimwidth() php function.

    $excerpt = mb_strimwidth( strip_tags(get_the_content()), 0, 100, '...' );
    

    Using add_filter() method of WordPress on the_content filter hook.

    add_filter( "the_content", "limit_content_chr" );
    function limit_content_chr( $content ){
        if ( 'post' == get_post_type() ) {
            return mb_strimwidth( strip_tags($content), 0, 100, '...' );
        } else {
            return $content;
        }
    }
    

    Using custom php function to limit content characters.

    function limit_content_chr( $content, $limit=100 ) {
        return mb_strimwidth( strip_tags($content), 0, $limit, '...' );
    }
    
    // using above function in template tags
    echo limit_content_chr( get_the_content(), 50 );
    
  6. just to help, if any one want to limit post length at home page .. then can use below code to do that..

    the below code is simply a modification of @bfred.it Sir

    add_filter("the_content", "break_text");
    
    function limit_text($text){
    
      if(is_front_page())
      {
        $length = 250;
        if(strlen($text)<$length+10) return $text; //don't cut if too short
        $break_pos = strpos($text, ' ', $length); //find next space after desired length
        $visible = substr($text, 0, $break_pos);
        return balanceTags($visible) . "... <a href='".get_permalink()."'>read more</a>";
      }else{
        return $text;
      }
    
    }
    
  7. I know this post is a bit old, but thought I would add the functions I use that take into account any filters and any <![CDATA[some stuff]]> content you want to safely exclude.

    Simply add to your functions.php file and use anywhere you would like, such as:

    content(53);

    or

    excerpt(27);

    Enjoy!

    //limit excerpt
    function excerpt($limit) {
      $excerpt = explode(' ', get_the_excerpt(), $limit);
      if (count($excerpt)>=$limit) {
        array_pop($excerpt);
        $excerpt = implode(" ",$excerpt).'...';
      } else {
        $excerpt = implode(" ",$excerpt);
      } 
      $excerpt = preg_replace('`[[^]]*]`','',$excerpt);
      return $excerpt;
    }
    
    //limit content 
    function content($limit) {
      $content = explode(' ', get_the_content(), $limit);
      if (count($content)>=$limit) {
        array_pop($content);
        $content = implode(" ",$content).'...';
      } else {
        $content = implode(" ",$content);
      } 
      $content = preg_replace('/[.+]/','', $content);
      $content = apply_filters('the_content', $content); 
      $content = str_replace(']]>', ']]&gt;', $content);
      return $content;
    }