Hi,
I have a question about the possibility to underline ( or give it some styles ) the words that result by search. I tried all methods seen online ( I wouldn’t like using plugins if it is possible! ) but works only on title ( the_title ). on excerpt ( the_excerpt ) don’t work. Have you any ideas to do this?
Thanks in advance,
Antonello
I put this code in function.php:
function highlight_results($text){
if(is_search()){
$sr = get_query_var(‘s’);
$keys = explode(” “,$sr);
$text = preg_replace(‘/(‘.implode(‘|’, $keys) .’)/iu’, ‘<strong class=”search-term”>’.$sr.’‘, $text);
}
return $text;
}
add_action(‘the_excerpt’, ‘highlight_results’);
add_action(‘the_title’, ‘highlight_results’);