When displaying post excerpts on your WordPress site, you may want to add a custom “Read More” link that appears at the end of the excerpt. This can be useful for improving the user experience and encouraging visitors to click through to read the full post. Here’s how you can customize the excerpt Read More link in WordPress:
- Use the
excerpt_more
filter to modify the default “Read More” text:
function wpsnippets_custom_excerpt_more( $more ) {
return 'Continue reading →';
}
add_filter( 'excerpt_more', 'wpsnippets_custom_excerpt_more' );
In this example, we’re using the excerpt_more
filter to change the default “Read More” text to “Continue reading →”. You can modify the text to suit your needs.
- Use the
excerpt_length
filter to control the length of the excerpt:
function wpsnippets_custom_excerpt_length( $length ) {
return 50;
}
add_filter( 'excerpt_length', 'wpsnippets_custom_excerpt_length' );
In this example, we’re using the excerpt_length
filter to set the length of the excerpt to 50 words. You can modify the number to suit your needs.
By combining these two WordPress filters, you can create a custom excerpt with a customized “Read More” link that encourages visitors to click through to read the full post.
Examples
Example #1: Change the Read More text
This code example shows how to change the default “Read More” text that appears at the end of the excerpt.
function wpsnippets_custom_excerpt_more( $more ) {
return 'Continue reading →';
}
add_filter( 'excerpt_more', 'wpsnippets_custom_excerpt_more' );
This code uses the excerpt_more
filter to modify the text that appears at the end of the excerpt. The wpsnippets_excerpt_more_text
function returns the new text, which in this case is “Continue reading”.
Example #2: Add a custom Read More link
This code example shows how to add a custom link to the end of the excerpt.
function wpsnippets_excerpt_more_link( $more ) {
global $post;
return ' <a href="' . get_permalink( $post->ID ) . '">Read more</a>';
}
add_filter( 'excerpt_more', 'wpsnippets_excerpt_more_link' );
This code uses the excerpt_more
filter to modify the text that appears at the end of the excerpt. The wpsnippets_excerpt_more_link
function returns the new text, which in this case is a link to the full post. The global $post
line is used to access the current post object, and the get_permalink
function is used to get the URL of the post.
Example #3: Remove the Read More link
This code example shows how to remove the Read More link entirely from the excerpt.
function wpsnippets_remove_excerpt_more_link() {
return '';
}
add_filter( 'excerpt_more', 'wpsnippets_remove_excerpt_more_link' );
This code uses the excerpt_more
filter to remove the text that appears at the end of the excerpt. The wpsnippets_remove_excerpt_more_link
function returns an empty string, effectively removing the link.