Contextual/highlight search results. h2 fix, empty search check.
authorLaury GvR <laury@gaslightmedia.com>
Thu, 18 Jun 2015 15:57:47 +0000 (11:57 -0400)
committerLaury GvR <laury@gaslightmedia.com>
Thu, 18 Jun 2015 15:57:47 +0000 (11:57 -0400)
functions.php
search.php

index ad6ee4b..d890cc5 100755 (executable)
@@ -114,5 +114,28 @@ function GLM_get_header() {
     }
 }
 
+//<!-- Start of the Contextual/Highlight Search functions -->
+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(); ?>
+    <div id="post-<?php the_ID() ?>" class="<?php thematic_post_class() ?>">
+      <?php thematic_postheader(); ?>
+      <div class="entry-content">
+        <?php thematic_content(); /* We will replace this next */ ?>
+      </div>
+      <?php thematic_postfooter(); ?>
+  </div><!-- .post -->
+  <?php endwhile;
+}
 
+add_action('thematic_searchloop', 'mytheme_search_loop');
+///<!-- End of the Contextual/Highlight Search functions -->
 ?>
index 04e0442..03ab183 100644 (file)
 <?php get_header(); ?>
 
 <div class="row">
+    
+    <?php if (get_search_query()) { ?> <!-- This is to ensure empty search results are filtered. -->
     <h2 class="search-result-header">Search Results for "<?php echo get_search_query(); ?>"</h2>
-    <div id="blog-posts-over" class="small-12 columns">
+        <div id="blog-posts-over" class="small-12 columns">
+            <?php if(have_posts()): $i = 1; while (have_posts() && $i < 6) : the_post(); ?>
+            <div class="content" id="blog-posts-container">
+                <div class="small-11 small-centered columns">
+                    <div class="row">
+                        <div class="small-12 columns">
+                            <article id="<?php the_ID()?>" <?php post_class()?>>
+                                    <h2 class="">Page: <a href="<?php the_permalink(); ?>"><?php the_title();?></a></h2>
+                                    <hr>
+                                <?php the_post_thumbnail(); ?>
+                                    
+                <!-- Start of the Contextual/Highlight Search -->
+                                <?php
+                                // Configuration
+                                $max_length = 400; // Max length in characters
+                                $min_padding = 30; // Min length in characters of the context to place around found search terms
 
-        <?php if(have_posts()): $i = 1; while (have_posts() && $i < 6) : the_post(); ?>
-        <div class="content" id="blog-posts-container">
-            <div class="small-11 small-centered columns">
-                <div class="row">
-                    <div class="small-12 columns">
+                                // 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))) : '');
 
-                        <article id="<?php the_ID()?>" <?php post_class()?>>
-                                <h2 class="">Page: <a href="<?php the_permalink(); ?>"><?php the_title();?></a></h2>
-                                <hr>
-                            <?php the_post_thumbnail(); ?>
-                            <?php echo the_advanced_excerpt('length=200&length_type=words&no_custom=1&ellipsis=%26hellip;'); ?>
-                            <footer class="entry-meta small-12 medium-6 medium-push-3 center">
-                                
-                                <?php $post_categories = wp_get_post_categories( get_the_ID() );
-                                $cats = array();
+                                // Search content for terms
+                                $terms = $wp_query->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', '<strong>$0</strong>', $output));
                                 ?>
 
-                            </footer>
-                            <hr>
-                        </article>
+                                <p class="search_result_context">
+                                  <?php echo $context ?>
+                                </p>
+                                <p>
+                                  <a href="<?php the_permalink() ?>" rel="bookmark">Read this entry</a>
+                                </p>  
+                <!-- End of the Contextual/Highlight Search -->
+                                
+                                <?php //echo the_advanced_excerpt('length=200&length_type=words&no_custom=1&ellipsis=%26hellip;'); ?>
+                                <footer class="entry-meta small-12 medium-6 medium-push-3 center">
+
+                                    <?php $post_categories = wp_get_post_categories( get_the_ID() );
+                                    $cats = array();
+
+                                    ?>
+
+                                </footer>
+                                <hr>
+                            </article>
+                        </div>
                     </div>
                 </div>
             </div>
+            <?php endwhile; ?>
+            <?php if ( function_exists('FoundationPress_pagination') ) { FoundationPress_pagination(); } else if ( is_paged() ) { ?>
+
+                    <nav id="post-nav">
+                            <div class="post-previous"><?php next_posts_link( __( '&larr; Older results', 'FoundationPress' ) ); ?></div>
+                            <div class="post-next"><?php previous_posts_link( __( 'Newer results &rarr;', 'FoundationPress' ) ); ?></div>
+                    </nav>
+            <?php } ?>
+            <?php else: ?>
+            <div id="post-404" class="noposts">
+                    <p><?php _e('Sorry, no results were found.');?></p>
+            </div><!-- /#post-404 -->
+            <?php endif;?>
         </div>
-        <?php endwhile; ?>
-        <?php if ( function_exists('FoundationPress_pagination') ) { FoundationPress_pagination(); } else if ( is_paged() ) { ?>
-
-               <nav id="post-nav">
-                       <div class="post-previous"><?php next_posts_link( __( '&larr; Older results', 'FoundationPress' ) ); ?></div>
-                       <div class="post-next"><?php previous_posts_link( __( 'Newer results &rarr;', 'FoundationPress' ) ); ?></div>
-               </nav>
-       <?php } ?>
-        <?php else: ?>
-        <div id="post-404" class="noposts">
-                <p><?php _e('Sorry, no results were found.');?></p>
-        </div><!-- /#post-404 -->
-        <?php endif;?>
-    </div>
+    <?php } else { ?>
+        <h2> Please enter a term to be searched.</h2>
+    <?php } ?>
         <?php get_footer(); ?>