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