The WordPress Theme Search Template and Page Template
<?php get_header(); ?> <div id="container"> <div id="content"> <div id="nav-above"> </div><!-- #nav-above --> <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>> </div><!-- #post-<?php the_ID(); ?> --> <div id="nav-below"> </div><!-- #nav-below --> </div><!-- #content --> </div><!-- #container --> <?php get_sidebar(); ?> <?php get_footer(); ?>
But, of course, each one is going to take it’s own different path.
The Search Template
In search.php
we’re going to reintroduce the loop back into our Template. This time with 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 get_header(); ?> <div id="container"> <div id="content"> <?php if ( have_posts() ) : ?> <?php while ( have_posts() ) : the_post() ?> <!-- this is our loop --> <?php endwhile; ?> <?php else : ?> <!-- here's where we'll put a search form if there're no posts --> <?php endif; ?> </div><!-- #content --> </div><!-- #container --> <?php get_sidebar(); ?> <?php get_footer(); ?>
Pretty straightforward, right? Almost.
I like to keep all my index-ey Templates the same: Post Title, Meta, Content (or excerpt), Utility links. But when WordPress searches for posts it also searches through Pages, which don’t need the post meta or utility links displayed. So, in our loop, we’re going to check and see if we’re dealing with a post or a page.
<?php if ( $post->post_type == 'post' ) { ?> <?php } ?>
Wrap any code with that IF statement and it will only show if we’re dealing with a page. Now that we understand what’s going on, here’s what the #content div of our search template will look like:
<?php if ( have_posts() ) : ?> <h1><?php _e( 'Search Results for: ', 'your-theme' ); ?><span><?php the_search_query(); ?></span></h1> <?php global $wp_query; $total_pages = $wp_query->max_num_pages; if ( $total_pages > 1 ) { ?> <div id="nav-above"> <div><?php next_posts_link(__( '<span>«</span> Older posts', 'your-theme' )) ?></div> <div><?php previous_posts_link(__( 'Newer posts <span>»</span>', 'your-theme' )) ?></div> </div><!-- #nav-above --> <?php } ?> <?php while ( have_posts() ) : the_post() ?> <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>> <h2><a href="<?php the_permalink(); ?>" title="<?php printf( __('Permalink to %s', 'your-theme'), the_title_attribute('echo=0') ); ?>" rel="bookmark"><?php the_title(); ?></a></h2> <?php if ( $post->post_type == 'post' ) { ?> <div> <span><?php _e('By ', 'your-theme'); ?></span> <span><a href="<?php echo get_author_link( false, $authordata->ID, $authordata->user_nicename ); ?>" title="<?php printf( __( 'View all posts by %s', 'your-theme' ), $authordata->display_name ); ?>"><?php the_author(); ?></a></span> <span> | </span> <span><?php _e('Published ', 'your-theme'); ?></span> <span><abbr title="<?php the_time('Y-m-dTH:i:sO') ?>"><?php the_time( get_option( 'date_format' ) ); ?></abbr></span> <?php edit_post_link( __( 'Edit', 'your-theme' ), "<span class="meta-sep">|</span>ntttttt<span class="edit-link">", "</span>nttttt" ) ?> </div><!-- .entry-meta --> <?php } ?> <div> <?php the_excerpt( __( 'Continue reading <span>»</span>', 'your-theme' ) ); ?> <?php wp_link_pages('before=<div>' . __( 'Pages:', 'your-theme' ) . '&after=</div>') ?> </div><!-- .entry-summary --> <?php if ( $post->post_type == 'post' ) { ?> <div> <span><span><?php _e( 'Posted in ', 'your-theme' ); ?></span><?php echo get_the_category_list(', '); ?></span> <span> | </span> <?php the_tags( '<span><span>' . __('Tagged ', 'your-theme' ) . '</span>', ", ", "</span>ntttttt<span class="meta-sep">|</span>n" ) ?> <span><?php comments_popup_link( __( 'Leave a comment', 'your-theme' ), __( '1 Comment', 'your-theme' ), __( '% Comments', 'your-theme' ) ) ?></span> <?php edit_post_link( __( 'Edit', 'your-theme' ), "<span class="meta-sep">|</span>ntttttt<span class="edit-link">", "</span>ntttttn" ) ?> </div><!-- #entry-utility --> <?php } ?> </div><!-- #post-<?php the_ID(); ?> --> <?php endwhile; ?> <?php global $wp_query; $total_pages = $wp_query->max_num_pages; if ( $total_pages > 1 ) { ?> <div id="nav-below"> <div><?php next_posts_link(__( '<span>«</span> Older posts', 'your-theme' )) ?></div> <div><?php previous_posts_link(__( 'Newer posts <span>»</span>', 'your-theme' )) ?></div> </div><!-- #nav-below --> <?php } ?> <?php else : ?> <div id="post-0"> <h2><?php _e( 'Nothing Found', 'your-theme' ) ?></h2> <div> <p><?php _e( 'Sorry, but nothing matched your search criteria. Please try again with some different keywords.', 'your-theme' ); ?></p> <?php get_search_form(); ?> </div><!-- .entry-content --> </div> <?php endif; ?>
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 the usual trappings of a blog post.
Except comments. Sometimes pages have comments. I don’t like them there. You might one day. So we’ll have to deal with that somehow. How about with … custom fields. If you want comments on a page you can add a custom field with Name and Value of “comments” to your page. Sounds good to me.
Here’s what you’ll need to for a perfect WordPress Page Template:
<?php get_header(); ?> <div id="container"> <div id="content"> <?php the_post(); ?> <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>> <h1><?php the_title(); ?></h1> <div> <?php the_content(); ?> <?php wp_link_pages('before=<div>' . __( 'Pages:', 'your-theme' ) . '&after=</div>') ?> <?php edit_post_link( __( 'Edit', 'your-theme' ), '<span>', '</span>' ) ?> </div><!-- .entry-content --> </div><!-- #post-<?php the_ID(); ?> --> <?php if ( get_post_custom_values('comments') ) comments_template() // Add a custom field with Name and Value of "comments" to enable comments on this page ?> </div><!-- #content --> </div><!-- #container --> <?php get_sidebar(); ?> <?php get_footer(); ?>
How To Create a WordPress Theme
This post is part of a WordPress Themes Tutorial that will show you how to create a powerful WordPress Theme from scratch. Read it from the beginning and code yourself up something awesome.