Time for action – creating a search.php template file

Let's create our search.php file and add some code to get it working in the way we'd like it to:

  1. In your theme folder, make a copy of index.php and call it search.php.
  2. Find the following code near the top of the file:
    <h2 class="thisMonth embossed" style="color:#fff;">This Month:</h2>
  3. Edit the contents of the h2 element so the line of code now reads:
    <h2 class="thisMonth embossed" style="color:#fff;">Search results:</h2>
  4. Find the loop. This will begin with:
    <?php if (have_posts()) :?>
      <?php while (have_posts()) : the_post();?>
  5. The first section of the loop displays any posts found by the search, leave this as it is. The second section of the loop specifies what happens if no search results are found. It's in the following lines of code:
    <?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; ?>
  6. Underneath the line that reads <?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;   ?>
  7. Save your search.php file and try searching for something which isn't included in the site.

What just happened?

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:

  • The function $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.
  • We gave 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.
  • We then output the title of each post with the 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.
  • Finally, we added 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.

Note

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:

What just happened?

Next, an unsuccessful search:

What just happened?

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.

Creating a custom page template

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.32.230