How To Display Search Term And Result counts in WordPress

How To Display Search Term And Result counts in WordPress

This article talks about How To Display Search Term And Result counts in WordPress. While working In WordPress, some of you may want to display the number of results for the search term. You have to add a simple function on your search page to display the search term and the result count.

Manually you can generate codes for everything you want. The best thing about coding is that it does not put extra load on your website to place the code. Unlike the plugins, which function as well, it puts a burden on your website.

Some Plus Points of showing search term and result count 

The advantage of showing the search term with a result count is that it will give the search term details to the visitors about how many posts are on your website for a particular topic.

The more options visitors get on your particular site, the more the chances of staying on your site. Moreover, it will engage the visitors to your site and help to enhance your SEO ranking.

You can do this by using codes.

Now, Open your search.php file in your theme option and place 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 will display something like this:

Search Result for WordPress Plugin — 10 articles

Also, You can highlight the search term by adding the .search-terms CSS class to your theme’s stylesheet. Below is a simple CSS for you.

 .search-terms {

background-color:yellow;

color:blue;

}

To conclude, This was the simple and easy code you can use to display the search term and result count in WordPress.

In this section, we have also mentioned how you can highlight the search term, which helps to grab attention readily. Showing the search term with the result count benefits the visitors as they know about the total articles on a particular topic.

We hope this has helped you well.

You can check our another article on how to display the most accurate comment count on WordPress.

Leave a Comment