I have a custom field added through the Advanced Custom Fields Plugin. I am trying to trim the content of the field down to a set number of words. I have tried to use the excerpt filter and setting a custom length see code bellow but It's not working, thought?

// set the length of the excerpt
function custom_excerpt_length( $length ) 
    return 20;

add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

// apply the filter and strip crap from 'description'
// http://codex.wordpress.org/Function_Reference/the_content
$content = apply_filters('the_excerpt', get_field('description'));
$content = str_replace(']]>', ']]>', $content);

echo $content;

//echo wp_trim_excerpt($content);

I also tried the wp_trim_except() function but it isn't doing anything either. The whole length of the text is being echoed.

1 Answer 1


The excerpt is a specific field in WordPress and is wholly unrelated to custom fields, which is why the excerpt stuff you tried has no effect.

There is no built in way to trim custom fields, you have to do it manually with a bit of php:

$length = 20;
$text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec diam enim, egestas ut facilisis sit amet, semper sed turpis. Phasellus dolor nisl, pretium non imperdiet nec, cursus quis turpis. Duis sed lorem tortor, a gravida magna. Integer commodo cursus ante, non varius odio aliquam quis. Aenean tincidunt congue ultricies. Donec ut orci vel ante laoreet bibendum. Aliquam vitae erat arcu. Nam et mi nunc. Proin sed lorem ac odio vestibulum blandit sit amet id dolor. Sed in augue id massa feugiat convallis.';
$words = explode(' ', $text);
array_splice($words, $length);
$text = implode(' ', $words);
echo $text;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.