Latest Tech and Tips

How to Highlight the Search Terms in Results in WordPress

0

In an try to make your WordPress search much more person pleasant, you possibly can spotlight the search phrases in the outcomes. We did this for one among our shoppers, so we thought it could be helpful for different customers. In this text we’ll present you how one can spotlight search phrases in the outcomes in WordPress.

Highlighting search terms in WordPress search results

First open your search.php and search for the following code:

<?php the_title(); ?>

Replace the above code with:

<?php echo $title; ?>

Make certain that you just paste this line above the title code:

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

Now open your CSS file and add the styling for the class search-excerpt, and it’ll spotlight the time period. Currently the code is making the search phrases daring. You can do that easy CSS in your theme’s stylesheet.

sturdy.search-excerpt { 
background-color:yellow;
colour:blue;
}

Source: Michael Martin