Archived topic
Automatically generate post_excerpt from post_content
8 replies · Started by Ernst Wilhelm on October 31, 2021
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
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. :D
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
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.
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?
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?
Hi. Puzzling with your code snippets I was not too successful. I leave it for now but don't worry.
Let us know if you need further help. :D
Thank you :)