0

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.

1 Answer 1

2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.