The WordPress Theme Search Template and Page Template

The Search Template and The Page Template are vital to any complete WordPress Theme. And they’re both really easy to code.

For both of these Templates, we’ll start with our template-template, single.php, again. As a refresher, here’s single.php.

<?php
/**
 * The Template for displaying all single posts.
 *
 * @package Shape
 * @since Shape 1.0
 */

get_header(); ?>

		<div id="primary" class="content-area">
			<div id="content" class="site-content" role="main">

			<?php while ( have_posts() ) : the_post(); ?>

				<?php shape_content_nav( 'nav-above' ); ?>

				<?php get_template_part( 'content', 'single' ); ?>

				<?php shape_content_nav( 'nav-below' ); ?>

				<?php
					// If comments are open or we have at least one comment, load up the comment template
					if ( comments_open() || '0' != get_comments_number() )
						comments_template( '', true );
				?>

			<?php endwhile; // end of the loop. ?>

			</div><!-- #content .site-content -->
		</div><!-- #primary .content-area -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

But, of course, each one is going to take its own path.

The Search Template

In search.php we’re going to wrap our loop inside an IF statement—in case we don’t have any posts to loop through.

Here’s how it’ll work: IF we have posts, or, in other words, if there are posts that match the terms we’re searching for, THEN loop through them, sorta just like in index.php, but IF we don’t have posts to loop through, or, if there aren’t any posts that match our search terms, give our searchers another chance at this searching business.

In code, it would look like this:

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

Pretty straightforward, right? Let’s cover a few areas in the code.

First, take a look at everything between if ( have_posts() ) : (line 14) and shape_content_nav( 'nav-below' );. If there are search results, this is the markup that controls how the search results page will look. We’ll get a nice title at the top that includes the search term, followed by a listing of the posts returned in the search.

Also, shape_content_nav() works the same way here as it does in index.php.

You’ll remember get_template_part() from earlier lessons. It’s loading the file containing the actual markup for the posts returned in the search. It’ll first look for a file called “content-search.php”, but since we are not creating that file, it’ll use content.php instead. If you wanted to format your search results posts differently (just like we did for Aside posts), feel free to create a “content-search.php” file and go wild!

Next, let’s look at the code between else : (line 31) and endif; (line 35). If there are no search results to display, we want to let our visitor know, nicely. To do that, we load the same no-results.php file that we created in the index.php lesson. It sure feels great when we can resuse code without needing to actually paste it into each file.

The Search Form Template
Back in WordPress Theme Template & Directory Structure, we added searchform.php. If you want to customize the markup of your theme’s search form, this is the file you’d use to make that happen. Otherwise, searchform.php is optional. If your theme doesn’t have a searchform.php, then WordPress displays the search form with its own built-in markup.

Here’s the code to paste into searchform.php.

<?php
/**
 * The template for displaying search forms in Shape
 *
 * @package Shape
 * @since Shape 1.0
 */
?>
	<form method="get" id="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>" role="search">
		<label for="s" class="assistive-text"><?php _e( 'Search', 'shape' ); ?></label>
		<input type="text" class="field" name="s" value="<?php echo esc_attr( get_search_query() ); ?>" id="s" placeholder="<?php esc_attr_e( 'Search &hellip;', 'shape' ); ?>" />
		<input type="submit" class="submit" name="submit" id="searchsubmit" value="<?php esc_attr_e( 'Search', 'shape' ); ?>" />
	</form>

The Page Template

You know what the Page Template is for. WordPress thinks of it as a post out of chronological order. We think of it as, well a page. But largely, it’s defined by what it doesn’t have: all of the usual metadata (such as navigation, timestamps, categories, and tags) included a blog post.

Our Page template file will look very much like single.php. We will also place the contents of the Page loop inside content-page.php.

First, let’s set up the Page template. Open page.php and add the following code.

<?php
/**
 * The template for displaying all pages.
 *
 * This is the template that displays all pages by default.
 * Please note that this is the WordPress construct of pages
 * and that other 'pages' on your WordPress site will use a
 * different template.
 *
 * @package Shape
 * @since Shape 1.0
 */

get_header(); ?>

		<div id="primary" class="content-area">
			<div id="content" class="site-content" role="main">

				<?php while ( have_posts() ) : the_post(); ?>

					<?php get_template_part( 'content', 'page' ); ?>

					<?php comments_template( '', true ); ?>

				<?php endwhile; // end of the loop. ?>

			</div><!-- #content .site-content -->
		</div><!-- #primary .content-area -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

Now, let’s place the markup for the actual Page inside content-page.php. Paste the following into content-page.php.

<?php
/**
 * The template used for displaying page content in page.php
 *
 * @package Shape
 * @since Shape 1.0
 */
?>

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
	<header class="entry-header">
		<h1 class="entry-title"><?php the_title(); ?></h1>
	</header><!-- .entry-header -->

	<div class="entry-content">
		<?php the_content(); ?>
		<?php wp_link_pages( array( 'before' => '<div class="page-links">' . __( 'Pages:', 'shape' ), 'after' => '</div>' ) ); ?>
		<?php edit_post_link( __( 'Edit', 'shape' ), '<span class="edit-link">', '</span>' ); ?>
	</div><!-- .entry-content -->
</article><!-- #post-<?php the_ID(); ?> -->

It’s a stripped-down version of content-single.php. We’ve omitted all of the metadata that’s associated with posts.

And with that, you’re done with this lesson. Enjoy your new Page and Search templates.

How To Create a WordPress Theme

This post is part of the The ThemeShaper WordPress Theme Tutorial: 2nd Edition that will show you how to create a powerful WordPress Theme from scratch. Read it from the beginning and code yourself up something awesome.

  1. WordPress Theme Tutorial Introduction
  2. Developing Theme Sense
  3. Theme Development Tools
  4. Creating a Theme HTML Structure
  5. Template and Directory Structure
  6. Setting Up Your Theme Functions
  7. Secure Your WordPress Theme
  8. The Header Template
  9. The Index Template
  10. The Single Post, Post Attachment, & 404 Templates
  11. The Comments Template
  12. The Search Template & The Page Template
  13. The Archive Template
  14. The Sidebar Template & The Footer Template
  15. Reset-Rebuild Theme CSS & Define Your Layouts
  16. Custom Background & Custom Header
  17. Distributing Your WordPress Theme

5 responses

  1. roberto tessare Avatar
    roberto tessare

    Hello! Great tutorial so far! Very easy to follow! i do have a question, though: How do I show my search box in the front page ? Think Google’s front page here (of course i won’t be using it the way they do but i want it to be more prominent).

  2. I’ve been following along this tutorial and exploring the _s theme while I do so. Will you be adding any tutorials on how to integrate custom page templates back into the theme?

  3. […] the original: The WordPress Theme Search Template and Page Template … This entry was posted in WordPress Guide and tagged archives, css, design, firefox, podcast, […]

  4. How can I create new page templates with different layout.css options? Do I just link the page template to the corresponding stylesheet? Thanks for your help and for this theme which is teaching me a lot.

  5. I have metro.php and i use get_templater_part(‘metro);
    now it is working on wp 3.6.1.
    thanks for sharing this post, very helpfull for me.
    I am new in wordpress theme ddevelopment