Display Search Term and Result Count in WordPress

148

In this tutorial we will share how you can add a simple function on your search page which will display the search term and the number of results. This feature was a special request by one of our user via email. If you want us to cover a topic, then feel free to make a suggestion.

Displaying search term and result count in WordPress search

Open your search.php file in your theme and add the following code:

  <h2 class="pagetitle">Search Result for <?php /* Search Count */ $allsearch = &new WP_Query("s=$s&showposts=-1"); $key = wp_specialchars($s, 1); $count = $allsearch->post_count; _e(''); _e('<span class="search-terms">'); echo $key; _e('</span>'); _e(' &mdash; '); echo $count . ' '; _e('articles'); wp_reset_query(); ?></h2>

The code above will display something like this:

Search Result for twitter — 15 articles

You can also highlight the search term by adding .search-terms CSS class to your theme’s stylesheet. Here is a simple CSS to get you started:

  .search-terms {  background-color:yellow;  color:blue;  }

This is just one of the cool things that you can do for your Search Page when customizing it. You can also highlight search terms in the results, and even add a search by category feature to your WordPress search.

Source: Michael Martin