This code snippet allows you to customize the “Read More” text for excerpts in WordPress. By default, WordPress adds a “Read More” link at the end of excerpts to indicate that there is more content to be displayed. With this code, you can change the text of the “Read More” link to your own custom text.
The code defines a function wpcode_snippets_change_read_more
that takes two parameters: $read_more
and $read_more_text
. The $read_more
parameter represents the HTML markup of the “Read More” link, and the $read_more_text
parameter contains the default text “Read More”.
Inside the function, you can modify the line $custom_text = 'Read the whole post';
to set your desired custom text. Replace ‘Read the whole post’ with your preferred text.
The function uses the str_replace
function to replace the default “Read More” text with the custom text. The modified $read_more
string is then returned.
To change the “Read More” text for excerpts, add this code to your theme’s functions.php file. It applies the filter the_content_more_link
with a priority of 15 and two parameters, allowing you to customize the “Read More” text.
function wpcoder_snippets_change_read_more( $read_more, $read_more_text ) {
// Edit the line below to add your own "Read More" text.
$custom_text = 'Read the whole post';
$read_more = str_replace( $read_more_text, $custom_text, $read_more );
return $read_more;
}
add_filter( 'the_content_more_link', 'wpcoder_snippets_change_read_more', 15, 2 );