From: Laury GvR Date: Thu, 18 Jun 2015 15:57:47 +0000 (-0400) Subject: Contextual/highlight search results. h2 fix, empty search check. X-Git-Tag: v1.0.0~21 X-Git-Url: http://cvs2.gaslightmedia.com/gitweb/?a=commitdiff_plain;h=deba44727925e21e54b6364eb4a0043108422ce7;p=WP-Themes%2FRecycleEmmet.git Contextual/highlight search results. h2 fix, empty search check. --- diff --git a/functions.php b/functions.php index ad6ee4b..d890cc5 100755 --- a/functions.php +++ b/functions.php @@ -114,5 +114,28 @@ function GLM_get_header() { } } +// +function mytheme_init() { + remove_action('thematic_searchloop', 'thematic_search_loop'); +} + +add_action('init', 'mytheme_init', 10); + +function mytheme_search_loop() { + while ( have_posts() ) : the_post(); ?> + +
+ +
+ + +
+ +
+ + ?> diff --git a/search.php b/search.php index 04e0442..03ab183 100644 --- a/search.php +++ b/search.php @@ -3,46 +3,105 @@
+ +

Search Results for ""

-
+
+ +
+
+
+
+
> +

Page:

+
+ + + + -
-
-
-
+ // Load content as plain text + global $wp_query, $post; + $content = (!post_password_required($post) ? strip_tags(preg_replace(array("/\r?\n/", '@<\s*(p|br\s*/?)\s*>@'), array(' ', "\n"), apply_filters('the_content', $post->post_content))) : ''); -
> -

Page:

-
- - -
- - query_vars['search_terms']; + if ( preg_match_all('/'.str_replace('/', '\/', join('|', $terms)).'/i', $content, $matches, PREG_OFFSET_CAPTURE) ) { + $padding = max($min_padding, $max_length / (2*count($matches[0]))); + + // Construct extract containing context for each term + $output = ''; + $last_offset = 0; + foreach ( $matches[0] as $match ) { + list($string, $offset) = $match; + $start = $offset-$padding; + $end = $offset+strlen($string)+$padding; + // Preserve whole words + while ( $start > 1 && preg_match('/[A-Za-z0-9\'"-]/', $content{$start-1}) ) $start--; + while ( $end < strlen($content)-1 && preg_match('/[A-Za-z0-9\'"-]/', $content{$end}) ) $end++; + $start = max($start, $last_offset); + $context = substr($content, $start, $end-$start); + if ( $start > $last_offset ) $context = '...'.$context; + $output .= $context; + $last_offset = $end; + } + + if ( $last_offset != strlen($content)-1 ) $output .= '...'; + } else { + $output = $content; + } + + if ( strlen($output) > $max_length ) { + $end = $max_length-3; + while ( $end > 1 && preg_match('/[A-Za-z0-9\'"-]/', $output{$end-1}) ) $end--; + $output = substr($output, 0, $end) . '...'; + } + // Highlight matches + $context = nl2br(preg_replace('/'.str_replace('/', '\/', join('|', $terms)).'/i', '$0', $output)); ?> -
-
-
+

+ +

+

+ Read this entry +

+ + + +
+ + + +
+
+
+
+ + + + + + +
+

+
+
- - - - - - -
-

-
- -
+ +

Please enter a term to be searched.

+