Support

Account

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

Solved

Truncating a custom field content based on CHARACTER count

  • Hi all,
    New to the ACF scene and just started using it. Read a bunch of documentation and trying to catch up on my PHP/WordPress literature. However, I stumbled on a great function that will pull a custom field and truncate it depending on the word count. However, I need to convert this to character count and am not sure the best way of doing so.

    within functions.php

    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);
        $excerpt_length = 50;  // 50 words
        $excerpt_more = ' ...';
        $text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
      }
      return apply_filters('the_excerpt', $text);
    }

    within template file
    <?php echo project_excerpt(); ?>

    In a nutshell, I create a variable for the custom field “project_description” called $text. If the text contains content, strip short codes, apply similar filters as the_content has, str_replace (not exactly sure, but it was included), set a length to 50 words (this is where I want a character count), add a excerpt suffix of “…” then trim the words based off the previously define filters.

    Hopefully someone can shed a little help on this.

    Thanks!

  • 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);
    }
    
  • dkeeling, thanks so much for your assistance on this! I greatly appreciate it!

    I have one additional questions — is there an easy way to strip the styling of the field being brought in? The client has some bolding/italicizing of text that I need to be stripped during the function you suggested above.

  • No problem @jodriscoll. πŸ™‚

    You can use strip_tags() to remove all tags. You can also choose to allow specific tags. Here’s a link about that…

    The updated code would then be:

    
    function project_excerpt() {
        global $post;
        $text = get_field('project_description');
        if ( '' != $text ) {
          $text = strip_shortcodes( $text );
          $text = apply_filters('the_content', $text);
          $text = strip_tags($text);
          $text = str_replace(']]>', ']]>>', $text);
          $text_length = strlen($text); // Get text length (characters)
          $excerpt_length = 350;  // 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);
      }
    

    One other note: I left them in, but I’m not sure that you need the calls to apply_filters() in there if you’re stripping everything out. You could just eliminate the first line:

    $text = apply_filters('the_content', $text);

    …and change the last one to return $text.

  • This reply has been marked as private.
Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.