Support

Account

Home Forums Front-end Issues Truncating a custom field content based on CHARACTER count Reply To: Truncating a custom field content based on CHARACTER count

  • Rather than use wp_trim_words(), you could use PHP’s substr() like this:

    
    function project_excerpt() {
      global $post;
      $text = get_field('project_description');
      if ( '' != $text ) {
        $text = strip_shortcodes( $text );
        $text = apply_filters('the_content', $text);
        $text = str_replace(']]>', ']]>>', $text);
        $text_length = strlen($text); // Get text length (characters)
        $excerpt_length = 50;  // 50 desired characters
        $excerpt_more = '...';
        
        // Shorten the text
        $text = substr($text, 0, $excerpt_length);
        
        // If the text is more than 50 characters, append $excerpt_more
        if ($text_length > $excerpt_length) {
          $text .= $excerpt_more;
        } 
    
      }
      return apply_filters('the_excerpt', $text);
    }