jmccole83
12/12/2017 - 5:59 PM

Truncate output custom field to specfic number of words using ACF Pro

Add truncate.php to the theme's functions.php file. Ommit the opening php tag. Edit line 10 to change the number of words output.

Add output.php to the template as required.

<?php echo custom_field_excerpt(); ?>
<?php 

function custom_field_excerpt() {
    global $post;
    $text = get_field('field_name');
    if ( '' != $text ) {
        $text = strip_shortcodes( $text );
        $text = apply_filters('the_content', $text);
        $text = str_replace(']]>', ']]>', $text);
        $excerpt_length = 12; // 12 words
        $excerpt_more = '...';
        $text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
    }
    return apply_filters('the_excerpt', $text);
}