Setting a default text for excerpts of a particular category

I am extremely new to WordPress Development and PHP in general, and was wondering if there is a way to create a function that would automatically add some pre-determined text to the excerpt of posts with a particular category.

If this is not possible, is there a way to set the excerpt to show just a particular pre-determined paragraph from the post itself which isn't the first paragraph in the actual post?

For context, all posts listed under this particular category are being generated from a database and are automatically posted once they are inputted on the WordPress db, so preferably when it is in the process of being published, the excerpt would be generated by this function and shown.

Thank you in advance.

Topic functions automation excerpt filters categories Wordpress

Category Web


Insert the following code, in functions.php of your child theme.

function wpse_excerpt_for_specific_category( $excerpt ) {
    global $post;
    $category = 'Test'; // could be name, slug, or id
    $excerpt_text = 'some arbitrary text';
    if( 'post' == $post->post_type && 'publish' == $post->post_status && in_category( $category ) ) {
        $excerpt = $excerpt_text;
    }
    return $excerpt;
}
add_filter( 'get_the_excerpt', 'wpse_excerpt_for_specific_category' );

Done.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.