Custom Excerpt Length

add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
/**
* Change the length of excerpt.
*
* @param int $length The number of words. Default 55.
* @return int New excerpt length.
*/
function custom_excerpt_length( $length ) {

    return 20; // number of words. Default is 55.
}

This function returns 20 words on the excerpt on my blog post. How can I customize this that instead of 20 words, it will return the first paragraph of my post.

Answer

Add the following function in functions.php file and it will return the first paragraph of post.

add_filter( 'wp_trim_excerpt', 'erginous_custom_excerpt', 10, 2 );

function erginous_custom_excerpt($text, $raw_excerpt) {
    if( ! $raw_excerpt ) {
        $content = apply_filters( 'the_content', get_the_content() );
        $text = substr( $content, 0, strpos( $content, '</p>' ) + 4 );
    }
    return $text;
}

This functions add a filter to modify the wp_trim_excerpt returned value by checking for the manual excerpt.