Please wait, loading...

 

Highlight Search Term in Search Results - Emerald Tech

December 28, 20160

Using wordpress native search, you can use this trick to highlight the search term in the title in the search results. This can be done by tokeinizing the title and then assigning a css class to the search word. This css class can be styled with background color to highlight the search word. Use this code in funtions.php

if(!empty($getvars[s])){
			$title = get_the_title();
			$keys= explode(" ",$s); $title = preg_replace('/('.implode('|', $keys) .')/iu', '<strong class="search-excerpt">\0</strong>', $title);
                        }
//To display the title use
echo $title;
//instead of
  the_title();

Last but not the least, paste this in your style.css

.search-excerpt{
background-color:#ffff00;
}

You can also use this plugin to achieve the same funtionality without writing any code yourself. Hope you like this wordpress snippet 🙂

Yumna Tatheer

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright @ Yamna Tatheer CEO Emerald Technologies Registration Number: 3520166158082