Get Excerpt from an Advanced Custom Field ACF

Place in your functions file:

// Custom Excerpt function for Advanced Custom Fields
function custom_field_excerpt() {
	global $post;
	$text = get_field('your_field_name'); //Replace 'your_field_name'
	if ( '' != $text ) {
		$text = strip_shortcodes( $text );
		$text = apply_filters('the_content', $text);
		$text = str_replace(']]>', ']]>', $text);
		$excerpt_length = 20; // 20 words
		$excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
		$text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
	}
	return apply_filters('the_excerpt', $text);
}

Make sure to change “your_field_name”.

Use in your template:

echo custom_field_excerpt();

 

4 thoughts on “Get Excerpt from an Advanced Custom Field ACF

  1. Carlos Arellano

    Really nice code!! i got a question though, its possible insert the permalink into the […]?

    thanks again!!
    Carlos.-

    1. acrane

      Sure, you would just need to get the permalink and then wrap it in the link. Add a new line to the function:

      $permalink = get_permalink($post->ID);

      That gives you the permalink. Then change the line that starts with $excerpt_more to:

      $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');

Comments are closed.