Support

Account

Home Forums General Issues Change default excerpt to custom post field

Solving

Change default excerpt to custom post field

  • I’m using ACF Pro on a custom Genesis site where all blog posts are using ACF fields instead of the default entry post content box. The post content is added by using sub fields within a Flexible Content type. WIthin that Flex Content field is a sub field for text and another for images).

    Not sure this makes a difference but I am also using the Genesis Featured Posts plugin as well on certain pages that pulls the post’s featured image, title, author and excerpt. Since I don’t have anything in the default post box, it generates nothing for excerpt. Ultimately I want to replace the excerpt everywhere with the content entered in the ACF Flex Content field I created. Is there a way to set that within my custom child theme that instructs WordPress to pull from that custom field instead of the default content entry box?

  • 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;
    }
  • break after the first correct row type, but reset the rows or you may have issues if you need to loop over the rows again

    
    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);
             // break to stop loop
             break;
            endif;
        endwhile;
        else:
        endif;
        reset_rows();
        return $my_acf_field . '' . $excerpt;
    }
    
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Change default excerpt to custom post field’ is closed to new replies.