Highlight Searched Term In WordPress Search Results

A popular way of displaying search results is to highlight the search term in the results. Read on for the code.

Search Result Code

  • Navigate to your template directory and open the search.php file
  • find the following code
the_title()
  • Replace it with the following code
echo $title;
  • The line before the previous code insert the following
<?php
	$title 	= get_the_title();
	$keys= explode(" ",$s);
	$title 	= preg_replace('/('.implode('|', $keys) .')/iu',
		'\0',
		$title);
?>
  • Open your themes stylesheet and add the following code
strong.search-excerpt { background: yellow; }

Let me know if this helped you! Leave a comment.

Leave a Reply