hightlight search results
This commit is contained in:
parent
e75c9ccfdc
commit
d9f02da9b9
2 changed files with 30 additions and 9 deletions
|
|
@ -387,12 +387,32 @@ add_action( 'widgets_init', 'kit_widgets_init' );
|
|||
|
||||
# Highlight query in search results
|
||||
|
||||
function kit_highlight_results($text){
|
||||
if(is_search()){
|
||||
$sr = get_query_var('s');
|
||||
$keys = explode(" ",$sr);
|
||||
$text = preg_replace('/('.implode('|', $keys) .')/iu', '<strong class="tx-indexedsearch-redMarkup">'.$sr.'</strong>', $text);
|
||||
}
|
||||
return $text;
|
||||
function highlight_search_term_placeholders() {
|
||||
static $iter = 0;
|
||||
$ret = "|##{$iter}##|";
|
||||
$iter++;
|
||||
return $ret;
|
||||
}
|
||||
add_filter('the_excerpt', 'kit_highlight_results');
|
||||
function highlight_search_term_cb() {
|
||||
static $iter = 0;
|
||||
$ret = "##{$iter}##";
|
||||
$iter++;
|
||||
return $ret;
|
||||
}
|
||||
function highlight_search_term($text){
|
||||
if(is_search() && !is_admin()){
|
||||
$keys = implode('|', explode(' ', get_search_query()));
|
||||
$pattern = '/<[^>].*?>/i';
|
||||
preg_match_all($pattern,$text,$matches);
|
||||
$placeholders = array();
|
||||
foreach ($matches[0] as $v) {
|
||||
$placeholders[] = highlight_search_term_placeholders();
|
||||
}
|
||||
$text = preg_replace_callback($pattern,'highlight_search_term_cb',$text);
|
||||
$pattern2 = '/(' . $keys .')/iu';
|
||||
$text = preg_replace($pattern2, ' <strong class="tx-indexedsearch-redMarkup">\1</strong> ', $text);
|
||||
$text = preg_replace($placeholders,$matches[0],$text);
|
||||
}
|
||||
return $text;
|
||||
}
|
||||
add_filter('the_excerpt', 'highlight_search_term');
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ get_header();
|
|||
<div id="c139" class="frame frame-default frame-type-list frame-layout-0">
|
||||
|
||||
<div class="tx-indexedsearch">
|
||||
|
||||
<div class="tx-indexedsearch-searchbox">
|
||||
<form method="get" role="search" action="<?php echo site_url(‘/’); ?>">
|
||||
<fieldset>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue