Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamiemitchell/72de32d985a664b12d341c050e178cc5 to your computer and use it in GitHub Desktop.
Save jamiemitchell/72de32d985a664b12d341c050e178cc5 to your computer and use it in GitHub Desktop.
Create a custom search template to display custom search results using SearchWP and the Genesis Framework.
<?php
/**
* This file adds the SearchWP Images template to your theme.
*
* Template Name: SearchWP Images
*
* @author Robert Neu
* @package Genesis
* @subpackage SearchWP
*/
/**
* This is a helper function to instantiate SearchWP.
*
* @return SearchwP object
* @since 1.0.0
*/
function prefix_searchwp_init() {
return SearchWP::instance();
}
/**
* This is a custom SearchWP form which submits using the "get" method.
*
* @param $query the user's search query
* @since 1.0.0
*/
function prefix_searchwp_form( $query ) {
if ( empty( $query ) ) {
$query = __( 'Search Images...', 'textdomain' );
}
echo'<form class="searchwp-form" action="" method="get">';
echo'<input type="text" id="swp-query" name="swp-query" value="' . esc_attr( $query ) . '" />';
echo'<input type="hidden" name="" id="" value="" />';
echo'<button type="submit">' . __( 'Search', 'textdomain' ) . '</button>';
echo'</form>';
}
/**
* Pagination for a Search WP search loop.
*
* This outputs basic wrapping HTML and displays pagination links for the user
* to navigate between pages of search results.
*
* @global $post
* @uses prefix_searchwp_init to instantiate the SearchWP class.
* @param $query the user's search query
* @param $page the current page number
* @since 1.1.0
*/
function prefix_searchwp_pagination( $query, $page ) {
if ( ! prefix_searchwp_init()->maxNumPages > 1 ) {
return;
}
// set up pagination
$prev = $page > 1 ? $page - 1 : false;
$next = $page < prefix_searchwp_init()->maxNumPages ? $page + 1 : false;
// Set the nav link for reuse.
$nav_link = get_permalink() . '?swp-query=' . urlencode( $query ) . '&amp;swp-page=';
?>
<!-- begin pagination -->
<div class="searchwp-pagination pagination">
<ul>
<?php if( $prev ) : ?>
<li class="next-prev nav-previous">
<a href="<?php echo $nav_link . $prev; ?>"><?php _e( '&larr; Previous Page', 'textdomain' ); ?></a>
</li>
<?php endif; ?>
<?php if( $next ) : ?>
<li class="next-prev nav-next">
<a href="<?php echo $nav_link . $next; ?>"><?php _e( 'Next Page &rarr;', 'textdomain' ); ?></a>
</li>
<?php endif; ?>
</ul>
</div>
<!-- end pagination -->
<?php
}
add_action( 'genesis_after_entry', 'prefix_do_search_loop' );
/**
* This is a custom loop which contains search results.
*
* It outputs basic wrapping HTML and displays images
* based on user's search queries.
*
* @global $post
* @uses prefix_searchwp_init to instantiate the SearchWP class.
* @since 1.0.0
*/
function prefix_do_search_loop() {
// Return early if SearchWP is disabled.
if ( ! class_exists( 'SearchWP' ) ) {
return;
}
global $post;
$counter = 0;
$query = isset( $_REQUEST['swp-query'] ) ? sanitize_text_field( $_REQUEST['swp-query'] ) : '';
$page = isset( $_REQUEST['swp-page'] ) ? absint( $_REQUEST['swp-page'] ) : 1;
// Load the custom SearchWP form.
prefix_searchwp_form( $query );
// Do nothing if no search has been performed.
if ( empty( $query ) ) {
return;
}
// perform the search
$posts = prefix_searchwp_init()->search( 'image_search', $query, $page );
// Display a message if there are no results.
if ( empty( $posts ) ) {
echo'<h2 class="entry-title search-title">' . __( 'Sorry, No Images Found. Try Another Search.', 'textdomain' ) . '</h2>';
return;
}
echo'<div class="searchwp-results">';
// Display the search results.
foreach ( $posts as $post ) {
// Make sure post template tags work correctly.
setup_postdata( $post );
$counter++;
$column_class = 'one-third';
// Add Last class to every 3rd post.
if ( $counter % 3 == 1 ) {
$column_class .= ' first';
}
echo '<article class="' . implode( ' ', get_post_class( $column_class ) ) . '">';
echo '<a class="search-image" href="'.wp_get_attachment_url( $post->ID ).' ">';
echo wp_get_attachment_image( $post->ID, 'in-post', false, array( 'title' => get_the_title() ) );
echo '</a><!-- .search-image -->';
echo '</article>';
}
wp_reset_postdata();
echo'</div>';
// Display pagination.
prefix_searchwp_pagination( $query, $page );
}
genesis();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment