如何在WordPress结果中突出显示搜索词

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的结果中突出显示搜索词。

Highlighting search terms in WordPress search results

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/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值