Posted By: Anonymous
I’ve spent a lot of time figuring out why my search is not working in my custom made template. So far I was able to figure out how to include searchform.php file in my header, created search.php file which is currently empty (so at the moment when I search for something I get redirected to a blank page and I think I definitely need something in search.php file to make it work), I was reading all around WordPress codex but could not find a solution, only useful information I found was this.
Can you suggest what need’s to be done in order to display results of a search? is there a special query, function etc? I really can’t find it anywhere.
my searchform.php file in case you need it.
<form action="<?php echo home_url(); ?>" id="search-form" method="get"> <input type="text" name="s" id="s" value="type your search" onblur="if(this.value=='')this.value='type your search'" onfocus="if(this.value=='type your search')this.value=''" /> <input type="hidden" value="submit" /> </form>
Basically, you need to include the WordPress loop in your search.php template to loop through the search results and show them as part of the template.
Below is a very basic example from The WordPress Theme Search Template and Page Template over at ThemeShaper.
<?php /** * The template for displaying Search Results pages. * * @package Shape * @since Shape 1.0 */ get_header(); ?> <section id="primary" class="content-area"> <div id="content" class="site-content" role="main"> <?php if ( have_posts() ) : ?> <header class="page-header"> <h1 class="page-title"><?php printf( __( 'Search Results for: %s', 'shape' ), '<span>' . get_search_query() . '</span>' ); ?></h1> </header><!-- .page-header --> <?php shape_content_nav( 'nav-above' ); ?> <?php /* Start the Loop */ ?> <?php while ( have_posts() ) : the_post(); ?> <?php get_template_part( 'content', 'search' ); ?> <?php endwhile; ?> <?php shape_content_nav( 'nav-below' ); ?> <?php else : ?> <?php get_template_part( 'no-results', 'search' ); ?> <?php endif; ?> </div><!-- #content .site-content --> </section><!-- #primary .content-area --> <?php get_sidebar(); ?> <?php get_footer(); ?>