How to Highlight Searched Text In Results

You can highlight searched text in search results. WordPress search system is powerfull but some things not acceptable for me :) i searched on internet and found this code for theme developers. You can add this special code to your theme. You need to change serach.php and your style.css.First open your search.php with your favourite php editor;

Find the_title() function and replace with

echo $title;

After than add this code to before modified line

<?php
 $title  = get_the_title();
 $keys= explode(" ",$s);
 $title  = preg_replace('/('.implode('|', $keys) .')/iu',
 '<strong class="search-excerpt">\0</strong>',  $title);
?>

And Last Step :)

Add this line to your style.css

strong.search-excerpt { background: yellow; }

You can change ‘yellow’ with a color code or color name.

That’s All! Have a nice day!


Related posts:

Jul 21st, 2009 | Posted in Wordpress
If you enjoyed this post, make sure you subscribe to my RSS Feed
Comments are closed.
Highslide for Wordpress Plugin