In an attempt to make your WordPress search even more user friendly, you can highlight the search terms in the results. We did this for one of our clients, so we thought it would be useful for other users. In this article we will show you how you can highlight search terms in the results in WordPress.
为了使您的WordPress搜索更加用户友好,您可以在结果中突出显示搜索词。 我们是为其中一位客户执行此操作的,因此我们认为这对其他用户很有用。 在本文中,我们将向您展示如何在WordPress的结果中突出显示搜索词。
First open your search.php and look for the following code:
首先打开您的search.php并查找以下代码:
<?php the_title(); ?>
Replace the above code with:
将上面的代码替换为:
<?php echo $title; ?>
Make sure that you paste this line above the title code:
确保将此行粘贴在标题代码上方:
<?php $title = get_the_title(); $keys= explode(" ",$s); $title = preg_replace('/('.implode('|', $keys) .')/iu', '<strong class="search-excerpt">\0</strong>', $title); ?>
Now open your CSS file and add the styling for the class search-excerpt, and it will highlight the term. Currently the code is making the search terms bold. You can try this simple CSS in your theme’s stylesheet.
现在打开您CSS文件,并为search-excerpt类添加样式,它将突出显示该术语。 目前,该代码使搜索字词变为粗体。 您可以在主题的样式表中尝试这种简单CSS。
strong.search-excerpt {
background-color:yellow;
color:blue;
}
Source: Michael Martin
资料来源: 迈克尔·马丁
翻译自: https://www.wpbeginner.com/wp-tutorials/how-to-highlight-the-search-terms-in-results-in-wordpress/