Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display Wordpress search results?

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.

http://codex.wordpress.org/Creating_a_Search_Page

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> 
like image 499
Ilja Avatar asked Feb 10 '13 20:02

Ilja


People also ask

How do I get search results in WordPress?

Ans easy way to add a search box is with the free Add Search to Menu plugin – just install and use the plugin settings to customize your search form and results. You can also add a search box directly to any page (via your child theme, as mentioned above) by using the core WordPress function “get_search_form();”.

How do I fix my WordPress search page?

If WordPress search is still showing a 404 error, then the search results page template is missing. To fix this, you can install a new theme or hire a WordPress developer to write a new search results page template.

How do I use php search in WordPress?

Customizing Your Search PageOpen your searchpage. php in a text editor and edit it there. Above the get_search_form() function for your searchform. php within the content div, you can add text to help visitors search your site.


2 Answers

you need to include the Wordpress loop in your search.php this 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(); ?> 
like image 86
Said Erraoudy Avatar answered Oct 06 '22 00:10

Said Erraoudy


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(); ?> 
like image 30
James Hebden Avatar answered Oct 06 '22 00:10

James Hebden