How to display Wordpress search results? How to display Wordpress search results? php php

How to display Wordpress search results?


you need to include the Wordpress loop in your search.phpthis is example

search.php template file:

<?php get_header(); ?><?php$s=get_search_query();$args = array(                's' =>$s            );    // The Query$the_query = new WP_Query( $args );if ( $the_query->have_posts() ) {        _e("<h2 style='font-weight:bold;color:#000'>Search Results for: ".get_query_var('s')."</h2>");        while ( $the_query->have_posts() ) {           $the_query->the_post();                 ?>                    <li>                        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>                    </li>                 <?php        }    }else{?>        <h2 style='font-weight:bold;color:#000'>Nothing Found</h2>        <div class="alert alert-info">          <p>Sorry, but nothing matched your search criteria. Please try again with some different keywords.</p>        </div><?php } ?><?php get_sidebar(); ?><?php get_footer(); ?>


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(); ?>


I am using searchform.php and search.php files as already mentioned, but here I provide the actual code.

Creating a Search Page codex page helps here and #Creating_a_Search_Page_Template shows the search query.

In my case I pass the $search_query arguments to the WP_Query Class (which can determine if is search query!). I then run The Loop to display the post information I want to, which in my case is the the_permalink and the_title.

Search box form:

<form class="search" method="get" action="<?php echo home_url(); ?>" role="search">  <input type="search" class="search-field" placeholder="<?php echo esc_attr_x( 'Search …', 'placeholder' ) ?>" value="<?php echo get_search_query() ?>" name="s" title="<?php echo esc_attr_x( 'Search for:', 'label' ) ?>" />  <button type="submit" role="button" class="btn btn-default right"/><span class="glyphicon glyphicon-search white"></span></button></form>

search.php template file:

<?php    global $query_string;    $query_args = explode("&", $query_string);    $search_query = array();    foreach($query_args as $key => $string) {      $query_split = explode("=", $string);      $search_query[$query_split[0]] = urldecode($query_split[1]);    } // foreach    $the_query = new WP_Query($search_query);    if ( $the_query->have_posts() ) :     ?>    <!-- the loop -->    <ul>        <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>        <li>            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>        </li>       <?php endwhile; ?>    </ul>    <!-- end of the loop -->    <?php wp_reset_postdata(); ?><?php else : ?>    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p><?php endif; ?>