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.
在本教程中,我们将分享如何在搜索页面上添加一个简单功能,该功能将显示搜索词和结果数量。 此功能是我们的一位用户通过电子邮件提出的特殊要求。 如果您希望我们涵盖一个主题,请随时提出建议 。
Open your search.php file in your theme and add the following code:
在主题中打开search.php文件,并添加以下代码:
<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(' — '); echo $count . ' '; _e('articles'); wp_reset_query(); ?></h2>
The code above will display something like this:
上面的代码将显示如下内容:
Search Result for twitter — 15 articles
Twitter的搜索结果— 15篇文章
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 CSS类来突出显示搜索词。 这是一个入门的简单CSS:
.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.
这只是自定义搜索页面时可以做的很酷的事情之一。 您还可以在结果中突出显示搜索词 ,甚至可以将分类搜索功能添加到WordPress搜索中。
Source: Michael Martin
资料来源: 迈克尔·马丁
翻译自: https://www.wpbeginner.com/wp-tutorials/display-search-term-and-result-count-in-wordpress/