wordpress页面_如何从WordPress搜索结果中排除页面

wordpress页面

By default, WordPress Search feature displays published posts and published pages in search results. Often when users are looking for something in a blog, it is most likely a post rather than a page. In this article, we will show you how to make your search more relevant and less crowded by excluding pages from WordPress search results.

默认情况下,WordPress搜索功能在搜索结果中显示已发布的帖子和已发布的页面。 通常,当用户在博客中查找内容时,它很可能是帖子而不是页面。 在本文中,我们将向您展示如何通过从WordPress搜索结果中排除页面来使搜索更相关,更不拥挤。

Exclude pages frm WordPress search results

First, you add the following code to your theme’s functions.php file or a site-specific plugin.

首先,将以下代码添加到主题的functions.php文件或特定站点的插件中


//Exclude pages from WordPress Search
if (!is_admin()) {
function wpb_search_filter($query) {
if ($query->is_search) {
$query->set('post_type', 'post');
}
return $query;
}
add_filter('pre_get_posts','wpb_search_filter');
}

Explanation: This code first checks that the search is not originating from the WordPress admin pages. If it is not, then it searches for posts by setting the post_type parameter.

说明:此代码首先检查搜索是否源自WordPress管理页面。 如果不是,那么它将通过设置post_type参数来搜索帖子。

You can also make it do the opposite by setting the post_type to pages, so it only return pages in the search results.

您还可以通过将post_type设置为pages来使其相反,因此它仅返回搜索结果中的页面。

We hope this article helped you learn how to exclude pages from WordPress search results. You may also want to see our list of best WordPress search plugins to improve your site search.

我们希望本文能帮助您学习如何从WordPress搜索结果中排除页面。 您可能还想查看我们的最佳WordPress搜索插件列表,以改善您的站点搜索。

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

如果您喜欢这篇文章,请订阅我们的YouTube频道 WordPress视频教程。 您也可以在TwitterFacebook上找到我们。

翻译自: https://www.wpbeginner.com/wp-tutorials/how-to-exclude-pages-from-wordpress-search-results/

wordpress页面

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值