Support

Account

Home Forums General Issues Change default excerpt to custom post field Reply To: Change default excerpt to custom post field

  • I’ve been able to get the below code to work but it selects the last instance of that particular sub field. When creating a post, the user will post content using Flexible content fields for blog_post_text, blog_post_images, and blog_post_gallery. Each post can have multiple. For instance, add one text sub field for the intro, then add an image sub field, then another text sub field, so on and so on. When I use this code, it creates the excerpt, limits it to 100 words, but selects the text from the last blog_post_text sub field added instead of the first.

    add_filter('the_excerpt', 'your_function_name');
    
    function your_function_name($excerpt) {
      if( have_rows('blog_post_content') ):
      
        while ( have_rows('blog_post_content') ) : the_row(); 
            if( get_row_layout() == 'blog_post_text_content' ): 
          
             $my_acf_field = wp_trim_words(get_sub_field('blog_post_text'), 100);
    
            endif;
        endwhile;
        else:
        endif;
        return $my_acf_field . '' . $excerpt;
    }