Auto generate excerpt from ACF field on a CPT that does not support excerpt or content

I have a CPT which contains only 4 wysiwyg fields created with ACF. The CPT does not support standard WP excerpt or content fields.

I would like the CPT to generate an excerpt automatically from one of the ACF fields.
I am not building a custom php template to output it from, so I need it to happen when get_the_excerpt is called within my theme. I want my custom excerpt to output on the themes index/archive pages.

I have got to the point that it works IF I include support for excerpt in the CPT AND there is something in the excerpt field… but it does not work if there is nothing in the excerpt field.

This is what I have done so far:

add_filter( ‘get_the_excerpt’, ‘tvw_vv_excerpt_filter’);

function tvw_vv_excerpt_filter( $excerpt, $post = null ){
if ( $post ) {
$ID = $post->ID;
} else {
$ID = get_the_ID();
}

$type = get_post_type($ID);
if ($type == “village_vibes”) {
$text = get_field(‘ceo_message’,$ID);
if ( ” != $text ) {
$text = strip_shortcodes( $text );
$text = apply_filters(‘the_content’, $text);
$text = str_replace(‘]]>’, ‘]]>’, $text);
$excerpt_length = 20;
$excerpt_more = apply_filters(‘excerpt_more’, ‘ ‘ . ‘[…]’);
$text = wp_trim_words( $text, $excerpt_length, $excerpt_more );

$excerpt = $text;
}
}

return $excerpt;
};

Question 1: Is it possible to make this work WITHOUT having an excerpt field in the CPT?

Question 2: If not, is it possible to make this work when the excerpt field is blank?

FYI – I am using Divi with a child theme.

Read more here:: Auto generate excerpt from ACF field on a CPT that does not support excerpt or content

Leave a Reply

Your email address will not be published. Required fields are marked *