Let's create our search.php
file and add some code to get it working in the way we'd like it to:
index.php
and call it search.php
.<h2 class="thisMonth embossed" style="color:#fff;">This Month:</h2>
h2
element so the line of code now reads:<h2 class="thisMonth embossed" style="color:#fff;">Search results:</h2>
<?php if (have_posts()) :?> <?php while (have_posts()) : the_post();?>
<?php else : ?> <h2 class="center">Not Found</h2> <p class="center">Sorry, but you are looking for something that isn't here.</p> <?php get_search_form(); ?> <?php endif; ?>
<?php get_search_form(); ?>
and before <?php endif; ?>
, add the following lines of code:<h3>Latest articles:</h3> <?php $query = new WP_Query( array ( 'post_type' => 'post', 'post_count' => '5' ) ); while ( $query->have_posts() ) : $query->the_post(); ?> <ul> <li> <a href="<?php the_permalink(); ?>"> <?php the_title(); ?> </a> </li> </ul> <?php endwhile; ?>
search.php
file and try searching for something which isn't included in the site.We created a new template file called search.php
, which will be used to display the results of a site search. We then edited the heading to make it clearer, and added some code to display the latest posts if the search had no results.
We actually did something pretty advanced, we added a second loop inside our original loop.
Let's have a look at the code we added after the search form:
$query = new WP_Query()
runs a new query on the database, based on the WordPress WP_Query
function, which is the function you should use when running a loop inside the main loop.WP_Query
the following parameters:'post_type' => 'post'
– this ensures that the query will only look for posts, not for any other kind of content.'post_count' => '5'
– this tells WordPress how many posts to show. Five, in this case.php the_title()
tag which we've already used higher up in the loop to display post titles. We wrapped this in a link inside a list item. The link uses the_permalink()
to link to the blog post whose title is displayed. This is very similar to the main loop.endwhile()
to stop this loop. This doesn't replace the endwhile()
at the end of our main loop, which is higher up in the file.For more on WP_Query and how to use it to create multiple loops, see http://codex.wordpress.org/Class_Reference/WP_Query.
Let's have a look at what our users will see when they do a search now. First, a successful search:
So that's how we set up a template file for search results. Our search page is only displaying two posts because that's all we have on our site. If there were more than five, it would just display the five most recent.
Now let's set one up to display some pages differently.
In many themes, all pages will need the same basic layout and content, with the same sidebars and footer and the same styling. But sometimes you may need some pages to look different.
For example, you might want to use different sidebars in different pages, or you might want a different layout. Here we'll look at the second of those two options.