加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > CMS系统 > wordpress > 正文

wordpress博客搜索关键词高亮显示实现代码

发布时间:2020-12-14 14:47:04 所属栏目:wordpress 来源:网络整理
导读:最近对wordpress的一些小改进很感兴趣,可以让你的博客更加个性,也更有趣味性,下面的代码可以在结果中高亮你所搜索的关键词。首先把第一段代码放在functions.php中: div class="msgborder" id="phpcode1" function hls_set_query() { $query = attribute_

最近对wordpress的一些小改进很感兴趣,可以让你的博客更加个性,也更有趣味性,下面的代码可以在结果中高亮你所搜索的关键词。首先把第一段代码放在functions.php中: <div class="msgborder" id="phpcode1"> function hls_set_query() { $query = attribute_escape(get_search_query()); if(strlen($query) > 0){ echo ' <script type="text/javascript"> var hls_query = "'.$query.'"; '; } } function hls_init_jquery() { wp_enqueue_script('jquery'); } add_action('init','hls_init_jquery'); add_action('wp_print_scripts','hls_set_query');  然后把下面的代码放在header.php中,就OK了。 <style type="text/css" media="screen"> .hls { background: #D3E18A; } <script type="text/javascript"> jQuery.fn.extend({ highlight: function(search,insensitive,hls_class){ var regex = new RegExp("(<[^>]>)|(b"+ search.replace(/([-.+?^${}()|[]/])/g,"$1") +")",insensitive ? "ig" : "g"); return this.html(this.html().replace(regex,function(a,b,c){ return (a.charAt(0) == "<") ? a : "<h3 class=""+ hls_class +"">" + c + ""; })); } }); jQuery(document).ready(function($){ if(typeof(hls_query) != 'undefined'){ $("#post-area").highlight(hls_query,1,"hls"); } });

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读