WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 1 down vote accepted

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;
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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