How to pull excerpt from Advanced Custom Field

//Add this code in your functions.php function advanced_custom_field_excerpt() { global $post; $text = get_field('your_field_name'); if ( '' != $text ) { $text = strip_shortcodes( $text ); $text = apply_filters('the_content', $text); $text = str_replace(']]>', ']]>', $text); $excerpt_length =30; // 30 words $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]'); $text = wp_trim_words( $text, $excerpt_length, $excerpt_more ); } return apply_filters('the_excerpt', $text); } //Add this line in your template <?php echo advanced_custom_field_excerpt(); ?>
Sometimes is useful to pull an excerpt from one of the custom fields using the plugin of Advanced Custom Field . This snippet helps you to get as an example the first 30 words as excerpt from the field 'your_field_name'

2 Responses

I do want to do this. But I'm not sure how to implement the <?php echo advanced_custom_field_excerpt(); ?> part.

Also, for the snippet below, I get an error. Do I need to add to add another "{", or delete a "}"?

function advanced_custom_field_excerpt() {
global $post;
$text = get_field('your_field_name');
if ( '' != $text ) {
$text = strip_shortcodes( $text );
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
$excerpt_length =30; // 30 words
$excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
$text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
}
return apply_filters('the_excerpt', $text);
}
I do want to do this. But I'm not sure how to implement the <?php echo advanced_custom_field_excerpt(); ?> part.

Also, for the snippet below, I get an error. Do I need to add to add another "{", or delete a "}"?

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

Write a comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.