0

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*\[&hellip;\]\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.

asked Oct 13 at 12:48
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.
10
  • 1
    That "Read more" text could be hard-coded. Have you checked the theme files yet? Commented Oct 13 at 13:07
  • Actually my full code will include checking statement to replace the customized text only to product items. 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. Commented Oct 13 at 13:52
  • What @cabrerahector is saying, is that your theme files may have "read more" in the actual file and not using any sort of filter/hook from WP. So no matter what you are doing, you cannot change the text, because it's not being output via the_excerpt or some other function. Do you have code example where you are trying to change it? Commented Oct 14 at 20:39
  • The latest code I have now is the one under "Note that my full code..." above where I put it in the theme's functions.php. Commented Oct 15 at 8:44
  • What theme are you using? Commented Oct 15 at 13:07

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.