If you use the custom post excerpt box, you can use HTML in there. Otherwise, you’d have to create new filters for the_excerpt
and stop it from stripping the tags; but that could result in some catastrophes on your site (for instance, if the link starts within the area that would be part of the excerpt, but ends outside of the excerpt area, you’d end up with an open link tag that never gets closed on your archive/home pages).
Thread Starter
x1code
(@secretja)
I understand this potential css problems but I can’t find how to make filter to stop striping html from excerpts. Any tip?
Thank you on reply.
You will first need to remove the wp_trim_excerpt
filter from get_the_excerpt
.
remove_filter( 'the_excerpt', 'wp_trim_excerpt' );
Then, you’ll need to write your own function that will perform most of the same actions that wp_trim_excerpt
performs. You can find the wp_trim_excerpt() function in wp-includes/formatting.php for reference. You might even just want to copy that function into your theme’s functions.php file, give it a different name (so you don’t have a function name clash) and then change the strip_tags($text)
call inside that function to strip_tags($text,'<a>')
.
Thread Starter
x1code
(@secretja)
Thank you on tips.
Here is how I do that.
function new_trim_excerpt($text) {
global $post;
if ( '' == $text ) {
$text = get_the_content('');
$text = apply_filters('the_content', $text);
$text = str_replace('\]\]\>', ']]>', $text);
$text = preg_replace('@<script[^>]*?>.*?</script>@si', '', $text);
$text = strip_tags($text, '<a>');
$excerpt_length = 80;
$words = explode(' ', $text, $excerpt_length + 1);
if (count($words)> $excerpt_length) {
array_pop($words);
array_push($words, '</a><a>ID) . '">Read the Rest...</a>');
$text = implode(' ', $words);
}
}
return $text;
}
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'new_trim_excerpt');