screenshot 1

You can highlight the search terms in the results to make your WordPress search even more user-friendly. We did this for one of our clients and thought it could be valuable to other people as well. In this article, we’ll teach you how to use WordPress to highlight search terms in the results.

Advertisements

Highlight

To begin, open your search.php file and locate the following code:

1
<?php the_title(); ?>
Advertisements

Substitute the following code for the above:

1
<?php echo $title; ?>

Make sure this line is pasted above the title code:

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

To highlight the word, enter your CSS file and apply the styling for the class search-excerpt. The search terms are currently bolded due to the code. You can use this simple CSS in the stylesheet of your theme.

1
2
3
4
strong.search-excerpt {
background-color:yellow;
color:blue;
}

 

Advertisements

Leave a Reply