search result count

We’ll show you how to add a simple function to your search page that displays the search keyword and the number of results in this tutorial. This functionality was requested by an email from one of our users. Make a proposal if you’d like us to cover a particular topic.

Advertisements

Displaying search term and result count in WordPress search

Add the following code to the search.php file of your theme:
1
<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>

 

Advertisements

The following is an example of what the code above will produce:

15 articles found when searching for twitter.

You may also use the.search-terms CSS class to highlight the search term in your theme’s stylesheet. To get you started, here’s a simple CSS:
1
2
3
4
.search-terms {
background-color:yellow;
color:blue;
}

This is just one of the interesting things you can do when customizing your Search Page. You can also add a search by category option to your WordPress search and highlight search terms in the results.

 

Advertisements

Leave a Reply