0

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 template to output it from, so I am just trying to filter get_the_excerpt whenever that is called within my theme. I want my custom excerpt to output on the themes index/archive pages or the default WP index page.

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.

I have seen and read a dozen or more examples of how to do this when outputting the result on a custom template, but found none describing how to achieve this automatically for a specified CPT using default templates.

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;
};

I also tried to filter the_content as it looks like it tries to get generate the excerpt from that if one does not exist in the excerpt field, however I cannot get it to return my custom field content. I assume this is because my CPT does not have "editor" support.. This is what I did to filter the the_content

function village_vibes_default_content($content) {
    global $post;

        if ($post->post_type == 'village_vibes') {
            $text = get_field('ceo_message');
            $content .= $text; 
    }
    return $content;
}
add_filter('the_content', 'village_vibes_default_content', 0);

Question: Is it possible to make this work WITHOUT having the content field/editor support in the CPT?

FYI - I am using Divi with a child theme.

2
  • Is my question not clear? Happy to update it following any questions. – mike_temby Feb 12 '20 at 4:12
  • Ok, then. I guess I'm on my own here... – mike_temby Feb 21 '20 at 0:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.