Am playing around with the standard WordPress search and am using this code in the functions file to highlight searched terms in the displayed results content.
function search_content_highlight() {$content = get_the_content();
$keys = implode('|', explode(' ', get_search_query()));
$content = preg_replace
('/(' . $keys .')/iu', '<strong class="search- highlight">\0</strong>', $content);
echo '<p>' . $content . '</p>';
}
Am using the content rather than excerpt so it always actually shows the needed word, but Id really love to trim the content, so it was just a dozen or so words either side of the searched word, which is held in the strong tags in the above code. Am pretty new to all this but am hoping someone can point me in the right direction if such a trim is possible.
Thanks in advance for any help!!
I am guessing that you are going to want to show a minimum of all of the bolded words. To do that you need to find where you find the first and last instance of a matched word.
I have tested the above code and it works. You might want to consider adding more logic in to have a max description size