- This topic has 8 replies, 2 voices, and was last updated 4 years, 5 months ago by
Ernst Wilhelm.
-
AuthorPosts
-
October 31, 2021 at 1:12 pm #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 youOctober 31, 2021 at 5:19 pm #1985851Elvin
StaffCustomer SupportHi 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. 😀
October 31, 2021 at 7:24 pm #1985911Ernst 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
October 31, 2021 at 7:40 pm #1985920Elvin
StaffCustomer SupportI 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.
October 31, 2021 at 8:10 pm #1985933Ernst 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?
October 31, 2021 at 8:31 pm #1985946Elvin
StaffCustomer SupportI 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?
November 12, 2021 at 2:17 am #2001144Ernst Wilhelm
Hi. Puzzling with your code snippets I was not too successful. I leave it for now but don’t worry.
November 14, 2021 at 4:22 pm #2004305Elvin
StaffCustomer SupportLet us know if you need further help. 😀
November 16, 2021 at 8:18 am #2006672Ernst Wilhelm
Thank you 🙂
-
AuthorPosts
- You must be logged in to reply to this topic.