How to Truncate and Add ‘Read More’ to Your WordPress Excerpt

Add any of the following lines of code to the functions.php file:

Code 1 – Read More
// Truncate excerpts and add 'Read More'
// Truncate excerpt
function custom_excerpt_length( $length ) {
return 70; // Number of words
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
// Add a "Read More" link to the end of an excerpt
function excerpt_read_more( $more ) {
return '... <a href="'.get_the_permalink().'" class="readmore">Read More...</a>';
}
add_filter( 'excerpt_more', 'excerpt_read_more' );
Code 2 – Read More
// Truncate excerpts and add 'Read More'
// Truncate excerpt
function custom_excerpt_length( $length ) {
return 70; // Number of words
}
// Add a "Read More" link to the end of an excerpt
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
// Add a "Read More" link to the end of an excerpt
function excerpt_read_more($more) {
global $post;
return '... <a class="readmore" href="'. get_permalink($post->ID) . '">Read More...</a>';
}
add_filter('excerpt_more', 'excerpt_read_more');
Code 3 – Read More
// Truncate excerpts and add 'Read More'
// Truncate excerpt
function custom_excerpt_length( $length ) {
return 70; // Number of words
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
// Add a "Read More" link to the end of an excerpt
function excerpt_read_more( $more ) {
if ( ! is_single() ) {
$more = sprintf( '... <a class="readmore" href="%1$s">%2$s</a>',
get_permalink( get_the_ID() ),
__( 'Read More...', 'textdomain' )
);
}
return $more;
}
add_filter( 'excerpt_more', 'excerpt_read_more' );
Displaying First Paragraphs as Excerpts

If you want to display the first paragraphs of articles as excerpts, add the following code to the functions.php file:

// Display first paragraphs as excerpts
 function wp_custom_excerpt($text, $raw_excerpt) {
    if( ! $raw_excerpt ) {
        $content = apply_filters( 'the_content', get_the_content() );
        $text = substr( $content, 0, strpos( $content, '</p>' ) + 4 );
    }
    $text = preg_replace("/<img[^>]+\>/i", "", $text); 
    $buttonmore = '<p><a class="btn btn-default" href="'. get_permalink($post->ID) . '"> Read More...</a></p>';
    return $text . " " . $buttonmore;
}
add_filter( 'wp_trim_excerpt', 'wp_custom_excerpt', 10, 2 );