Site logo

[Resolved] Automatically generate post_excerpt from post_content

Home Forums Support [Resolved] Automatically generate post_excerpt from post_content

Home Forums Support Automatically generate post_excerpt from post_content

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #1985730
    Ernst Wilhelm

    How can I transfer the first paragraph of the content (or a certain number of the first words) into the excerpt field? Our Authors are to lazy to write the excerpt by themselves. It would be useful to have this in place for our newsletter.
    Thank you

    #1985851
    Elvin
    Staff
    Customer Support

    Hi there,

    To clarify, do you only want to display the first paragraph of the post as excerpt on the post loops?

    Or perhaps force WordPress to get the paragraph and save it as a text for the manual excerpt text field?

    These 2 may seem the same but the latter option may affect previous posts that already have a manual excerpt.

    Let us know. 😀

    #1985911
    Ernst Wilhelm

    Hi

    I prefer the 2nd option. Just in case there is already text input in field excerpt there is nothing to be done.

    Thank you, Ernst Wilhelm

    #1985920
    Elvin
    Staff
    Customer Support

    I prefer the 2nd option. Just in case there is already text input in field excerpt there is nothing to be done.

    You can try this PHP snippet:

    add_action('generate_after_header',function(){
    	global $post;
    	
        $post_content = $post->post_content;
        $post_content = apply_filters('the_content', $post_content);
        $post_content = str_replace('</p>', '', $post_content);
        $paras = explode('<p>', $post_content);
    	array_shift($paras);
    	$first_paragraph = $paras[0];
    	
    	
    	$the_post = array(
          'ID'           => get_the_ID(),
          'post_excerpt' => $first_paragraph,
      	);
    	
    	var_dump($first_paragraph);
    		
    	if(is_single() && !has_excerpt()){		
    		echo 'no excerpt!';
    		wp_update_post( $the_post );
    	}
    });

    Warning: This will only work properly if your posts WITHOUT Read more block. It will get the first <p> element regardless if its a block or a custom HTML. This will take text of the first paragraph and save it to the excerpt of its post ONLY IF the post has an empty excerpt field. If you wish to reverse its effects, you’ll have to do it manually on the posts through its revision setting on the Block editor’s sidebar.

    #1985933
    Ernst Wilhelm

    I am realizing now that the manual excerpt is shown to its fully extent on the frontpage though I want to control this behaviour by a dedicated number of words, eg. 10 words (or 0 words).
    A manual excerpt is now shown fully regardless of its length. In case there is no manual excerpt due to historical reasons the first 10 words of the post_content is shown.

    We want to use the manual excerpt text field only for newsletter purposes.

    Any ideas?

    #1985946
    Elvin
    Staff
    Customer Support

    I am realizing now that the manual excerpt is shown to its fully extent on the frontpage though I want to control this behaviour by a dedicated number of words, eg. 10 words (or 0 words).

    The purpose of Manual excerpt was to let users write their own excerpts manually meaning this field is meant to take in and display the whole body of text placed on it.

    But if you wish to trim it, you can add this PHP snippet:

    add_filter( 'wp_trim_excerpt', 'tu_excerpt_metabox_more' );
    function tu_excerpt_metabox_more( $excerpt ) {
    	$generate_settings = wp_parse_args(
    			get_option( 'generate_blog_settings', array() ),
    			generate_blog_get_defaults()
    		);
    	
    	$excerpt_length = absint( apply_filters( 'generate_excerpt_length', $generate_settings['excerpt_length'] ) );
        $output = $excerpt;
    
        if ( has_excerpt() ) {
            $output = sprintf( '%1$s <p class="read-more-button-container"><a class="button" href="%2$s">%3$s</a></p>',
                wp_trim_words( $excerpt, $excerpt_length, ' ...' ),
                get_permalink(),
                __( 'Read more', 'generatepress' )
            );
        }
    	
        return $output;
    }

    And then set the excerpt length on Appearance > Customize > Layout > Blog.

    We want to use the manual excerpt text field only for newsletter purposes.

    Not sure what you mean by this. Do you mean there’s a post type for newsletter?

    #2001144
    Ernst Wilhelm

    Hi. Puzzling with your code snippets I was not too successful. I leave it for now but don’t worry.

    #2004305
    Elvin
    Staff
    Customer Support

    Let us know if you need further help. 😀

    #2006672
    Ernst Wilhelm

    Thank you 🙂

Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.