Last active
August 29, 2015 13:57
-
-
Save pommiegranit/9501629 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="content" class="col-full"> | |
<section id="main" class="col-left"> | |
<!-- begin search form --> | |
<form action="/library" method="post"> | |
<p> | |
<label for="query"><?php _e( 'Search', 'woothemes' ); ?></label> | |
<input type="text" name="query" id="query" value="<?php echo esc_attr( $query ); ?>" /> | |
</p> | |
<p> | |
<input type="submit" id="searchsubmit" value="<?php _e( 'Search', 'woothemes' ); ?>" /> | |
</p> | |
</form> | |
<!-- end search form --> | |
<?php | |
if( !empty( $query ) ) : | |
$product_cats = 'books, renew, sanctuary'; | |
$args = array( | |
'posts_per_page' => -1, | |
'post_type' => 'product', | |
'orderby' => 'date menu_order', | |
'order' => 'DESC', | |
's' => $query, | |
'sentence' => 1, | |
'product_cat' => $product_cats, | |
); | |
// perform the search | |
$posts = new WP_Query( $args ); | |
if( ( $posts->have_posts() ) ) : | |
?> | |
<header class="page-header"> | |
<h1 class="page-title"><?php printf( __( 'Library Search Results for: %s', 'woothemes' ), $query ); ?></h1> | |
</header> | |
<?php /* The loop */ ?> | |
<ul style="list-style: none"> | |
<?php | |
while ( $posts->have_posts() ): $posts->the_post(); | |
?> | |
<li style="display: block; margin-bottom: 50px"> | |
<div style="float: left; width: 110px;"> | |
<a href="<?php echo get_permalink(); ?>"> | |
<?php echo get_the_post_thumbnail( $post->ID, array( 175, 175 ) ); ?> | |
</a> | |
</div> | |
<div style="float:left; margin-left: 20px; width: 500px"> | |
<h2 style="margin-top: -10px; padding-top: 0px;"> | |
<a href="<?php echo get_permalink(); ?>" | |
<?php echo apply_highlight( get_the_title() , $query ) ?> | |
</a> | |
</h2> | |
<div><?php echo apply_highlight( get_snippet( get_the_content() , $query ) , $query ) ?></div> | |
</div> | |
<div style="clear:both"></div> | |
</li> | |
<?php | |
endwhile; | |
?> | |
</ul> | |
<?php | |
wp_reset_postdata(); | |
?> | |
<?php | |
else : | |
?> | |
<h1 class="page-title"><?php printf( __( 'Sorry, no matches found for "' . $query .'"', 'woothemes' ) ); ?></h1> | |
<h4>Search Suggestions:</h4> | |
<ul> | |
<li>Check your spelling</li> | |
<li>Try more general words</li> | |
<li>Try different words that mean the same thing</li> | |
</ul> | |
<h1 class="page-title">Or, perhaps these might be of interest...</h1> | |
<?php | |
echo do_shortcode('[product_category per_page="3" columns="3" orderby="date" order="desc" category="books"]'); | |
echo do_shortcode('[product_category per_page="3" columns="3" orderby="date" order="desc" category="renew"]'); | |
echo do_shortcode('[product_category per_page="3" columns="3" orderby="date" order="desc" category="sanctuary"]'); | |
endif; // !(empty ( $posts )) | |
endif; // !(empty ( $query )) | |
?> | |
</section><!-- /#main --> | |
</div><!-- /#content --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function apply_highlight( $the_content , $the_query) { | |
return preg_replace( '/' . $the_query . '/i' , '<span style="background-color: #00FF00">$0</span>' , $the_content ); | |
} | |
function get_snippet( $the_content , $the_query ) { | |
preg_match( '/' . $the_query . '/i' , $the_content , $matches, PREG_OFFSET_CAPTURE ); | |
$snippet = '<ul>'; | |
foreach ($matches as $match): | |
$cutoff = substr( $the_content, 0 , $match[1] ); | |
$start = strripos( $cutoff, '<li>' ); | |
$end = strpos( $the_content, '</li>' , $match[1] ); | |
$snippet .= substr( $the_content, $start, ( $end - $start ) + 4 ); | |
//$snippet .= $match[0] . ' - ' . $match[1]; | |
endforeach; | |
$snippet .= '</ul>'; | |
return $snippet; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment