Home » Labs » How to create a search page in wordpress  

How to create a search page in wordpress


Filed under Labs

Creating a search page is an essential part of WordPress theme development. Here is a post which contains how to create a search.php page in a better way. Search.php is the page where the theme will show all search results, so displaying search results we need to create two things, first is a search form and other one is search page. Below is the code for search form and search.php template in WordPress.

create a search form in WordPress

There are multiple ways to create a search form in WordPress here is one method. Create a searchform.php file in your theme folder and place and save the following php code in it.

 
 
<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">
    <div><label class="screen-reader-text" for="s"></label>
        <input type="text" value="" placeholder="Search..." name="s" id="s" />
        <input type="submit" id="searchsubmit" value="Search" />
    </div>
</form>

You can include the form in any where by calling the file in following way.

 
<?php get_search_form(); ?>

create a WordPress search page for showing results

Below is a example of search.php, create a php file called search.php in your theme folder, then copy and paste below code to it.

 
 
<?php
/*
Template Name: Search Page
*/
?>
 
<?php get_header(); ?>
 
	<section id="top" class="one">
 
		<div class="container">
 
			<div class="content">
 
	<?php if (have_posts()) : ?>
 
		<h2 class="pagetitle">Search Results for "<?php echo $s ?>"</h2>
 
		<div class="navigation">
			<div class="alignleft"><?php next_posts_link('&laquo; Previous') ?></div>
			<div class="alignright"><?php previous_posts_link('Next &raquo;') ?></div>
		</div>
 
		<div class="clear"></div>
 
		<?php while (have_posts()) : the_post(); ?>
 
			<div class="post" id="post-<?php the_ID(); ?>">
 
				<h1 class="large nomargin"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h1>
				<?php
				// Support for "Search Excerpt" plugin
				// http://fucoder.com/code/search-excerpt/
				if ( function_exists('the_excerpt') && is_search() ) {
					the_excerpt();
				} ?>
				<p class="small">
					<?php the_time('F jS, Y') ?> &nbsp;|&nbsp; 
					<!-- by <?php the_author() ?> -->
					Published in
					<?php the_category(', ');
						if($post->comment_count > 0) { 
								echo ' &nbsp;|&nbsp; ';
								comments_popup_link('', '1 Comment', '% Comments'); 
						}
					?>
				</p>
 
			</div>
 
			<hr>
		<?php endwhile; ?>
 
		<div class="navigation">
			<div class="alignleft"><?php next_posts_link('&laquo; Previous') ?></div>
			<div class="alignright"><?php previous_posts_link('Next &raquo;') ?></div>
		</div>
 
	<?php else : ?>
 
		<h2 class="center">No posts found. Try a different search?</h2>
		<?php include (TEMPLATEPATH . '/searchform.php'); ?>
 
	<?php endif; ?>
 
		</div> <!-- /container -->
	</div> <!-- /maincontent-->
 
	<?php get_sidebar(); ?>
 
	</section>
 
<?php get_footer(); ?>
It's only fair to share...Share on Google+Tweet about this on TwitterShare on Facebook

Posts you may like: