在WordPress中显示搜索字词和结果计数

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:

在主题中打开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(' &mdash; '); 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/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值