Always have “Read More” on Manual and Automatic excerpts
March 22, 2011 – 3:04 pm |Previously, we have talked about modifying your post excerpts. This is great if you always use the automatic excerpts that WordPress provides. However, if your customer every puts in a manual excerpt, this functionality won’t give you the “read more” that you are expecting. What a pain, right!?
Well, I have added a new function that I found here and modified it slightly to always have the read more link. Just add this to your theme’s functions.php file:
-
<?php
-
function new_excerpt_more($more) {
-
global $post;
-
return '… <a href="'. get_permalink($post->ID) . '">» Read More</a>';
-
}
-
add_filter('excerpt_more', 'new_excerpt_more');
-
-
function new_excerpt_length($length) {
-
return 120;
-
}
-
add_filter('excerpt_length', 'new_excerpt_length');
-
-
-
function wp_trim_all_excerpt($text) {
-
// Creates an excerpt if needed; and shortens the manual excerpt as well
-
global $post;
-
if ( empty($text) ) {
-
$text = get_the_content('');
-
}
-
$text = strip_shortcodes( $text ); // optional
-
$text = apply_filters('the_content', $text);
-
$text = str_replace(']]>', ']]>', $text);
-
-
$text = strip_tags($text);
-
$excerpt_length = apply_filters('excerpt_length', 55);
-
$excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
-
$words = preg_split("/[nrt ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
-
if (count($words)> $excerpt_length) {
-
array_pop($words);
-
$text = implode(' ', $words);
-
} else {
-
$text = implode(' ', $words);
-
}
-
$text = $text . $excerpt_more;
-
return $text;
-
}
-
-
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
-
add_filter('get_the_excerpt', 'wp_trim_all_excerpt');
-
?>
Note: This repeats the functionality from our previous post, do not copy/paste both into functions.php – you will see errors.

