Custom Read More link for WordPress

The default “read more” link for the wordpress that comes with the_excerpt function shows […] after every post listing WITHOUT a hyperlink to the post.  To make the_excerpt function more useful, you can add a custom read more link which will replace […]

It require a small tweak in wp_trim_excerpt function.

Open your wordpress file /wp-includes/formatting.php

Go to line number 1482. The wp_trim_excerpt function would read like this

BEFORE

function wp_trim_excerpt($text) {
if ( '' == $text ) {
$text = get_the_content('');

$text = strip_shortcodes( $text );

$text = apply_filters(‘the_content’, $text);
$text = str_replace(‘]]>’, ‘]]>’, $text);
$text = strip_tags($text);
$excerpt_length = apply_filters(‘excerpt_length’, 55);
$words = explode(‘ ‘, $text, $excerpt_length + 1);
if (count($words) > $excerpt_length) {
array_pop($words);
array_push($words, […]);
$text = implode(‘ ‘, $words);
}
}
return $text;
}

AFTER (refer to the modified text in red)

function wp_trim_excerpt($text) {
if ( ” == $text ) {
$text = get_the_content(”);
$read_link='<a href=”‘. get_permalink().'”>’;
$end_read_link=”</a>”;

$text = strip_shortcodes( $text );

$text = apply_filters(‘the_content’, $text);
$text = str_replace(‘]]>’, ‘]]&gt;’, $text);
$text = strip_tags($text);
$excerpt_length = apply_filters(‘excerpt_length’, 55);
$words = explode(‘ ‘, $text, $excerpt_length + 1);
if (count($words) > $excerpt_length) {
array_pop($words);
array_push($words, ‘…’.$read_link.'[read more]’.$end_read_link);
$text = implode(‘ ‘, $words);
}
}
return $text;
}

Cheers…you are done!

This entry was posted in Wordpress and tagged . Bookmark the permalink.

Leave a Reply

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