I tried to replace "Read more" text on the buttons at the end of each post in the post query loop, e.g. archive page, using 3 versions of codes below. None of them worked, the text still remained "Read more" the same.
// 1
function custom_search_read_more_text( $more ) {
global $post;
return '<a class="more-link" href="' . get_permalink( $post ) . '">View Product</a>';
}
add_filter( 'excerpt_more', 'custom_search_read_more_text' );
add_filter( 'the_content_more_link', 'custom_search_read_more_text' );
add_filter('the_excerpt', 'custom_search_read_more_text');
// 2
function new_excerpt_more($more) {
global $post;
return '<a class="more-link" href="' . get_permalink( $post->ID ) . '"> View Product </a>';
}
add_filter('excerpt_more', 'new_excerpt_more');
add_filter( 'the_content_more_link', 'new_excerpt_more' );
add_filter('the_excerpt', 'new_excerpt_more');
// 3
function custom_excerpt_read_more_link($text) {
global $post;
$text = preg_replace('/(\s*\[…\]\s*)|(\s*\[...\]\s*)/', '', $text);
$read_more_link = ' <a class="more-link" href="' . get_permalink( $post->ID ) . '">View Product</a>';
return $text . $read_more_link;
}
add_filter('excerpt_more', 'custom_excerpt_read_more_link');
add_filter( 'the_content_more_link', 'custom_excerpt_read_more_link' );
add_filter('the_excerpt', 'custom_excerpt_read_more_link');
Note that my full code will include checking statement to replace the customized text only to product items as below:
function custom_search_read_more_text( $more ) {
if ( is_search() ) {
global $post;
if ( 'product' === get_post_type( $post ) ) {
return ' <a class="more-link" href="' . get_permalink( $post ) . '">View Product</a>';
} else {
return ' <a class="more-link" href="' . get_permalink( $post ) . '">Read More</a>';
}
}
return $more;
}
add_filter( 'excerpt_more', 'custom_search_read_more_text' );
But as I tried just plain returning without the if, none of the above ever worked. So I simplified it here to make they read easier.
New contributor
FUFY is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
lang-php
the_excerpt
or some other function. Do you have code example where you are trying to change it?