Snippets Library

Add Read More Button in Excerpt

Posted by Editorial Staff

If you want to replace the default "Read More" ellipsis with a custom “Read More” button in WordPress excerpts, you can use the `excerpt_more` filter. Here’s an example:

function wpr_excerpt_more($more) {
// Change the "Read More" text to your desired button text
$read_more_text = 'Read More';
// Generate the custom button
$button = sprintf('… <a class="read-more" href="%1$s">%2$s</a>', get_permalink(), $read_more_text);
// Return only the custom button without the ellipsis
return $button;
add_filter('excerpt_more', 'wpr_excerpt_more');

In this example, the `wpr_excerpt_more` function sets the desired button text to “Read More”. The function generates a custom button using the `sprintf` function, which includes the link to the full post (`get_permalink()`) and the button text. The custom button is then appended after the ellipsis in the excerpt.

By adding the `add_filter('excerpt_more', 'wpr_excerpt_more');` line, you hook the `wpr_excerpt_more` function into the `excerpt_more` filter, and it will be called whenever the excerpt is generated.

You can modify the button text (`$read_more_text`) and customize the button’s HTML and CSS to match your desired style.


Leave a Reply

Your email address will not be published. Required fields are marked *