
WordPress 排除类别
您想从网站主页中排除特定类别吗?默认情况下,WordPress 在主页上显示所有类别的帖子。在某些情况下,您可能不希望这样。在本文中,我们将向您展示如何轻松地从 WordPress 主页中排除某个类别。
您想从网站主页中排除特定类别吗?默认情况下,WordPress 在主页上显示所有类别的帖子。在某些情况下,您可能不需要这样做。在本文中,我们将向您展示如何轻松地从 WordPress 主页中排除类别。

为什么要从WordPress主页中排除类别? (Why Exclude a Category from WordPress Homepage?)
WordPress 允许您将内容分类为类别和标签。现在,有时您可能想对不属于正常博客条目的帖子使用类别。
WordPress 允许您将内容分类为类别和标签。现在,有时您可能希望对非常规博客条目的帖子使用类别。
默认情况下,WordPress 不允许您从主页或档案中隐藏特定类别的帖子。这会让您的主页上出现不需要的帖子。
默认情况下,WordPress 不允许您从主页或档案中隐藏特定类别的帖子。这样,您的主页上就会出现不需要的帖子。
话虽这么说,让我们看看如何轻松地从 WordPress 主页中排除特定类别。我们将向您展示两种不同的方法,您可以选择最适合您需要的一种。
话虽如此,让我们看看如何轻松地从 WordPress 主页中排除特定类别。我们将向您展示两种不同的方法,您可以选择最适合您的一种。
方法1:使用插件从WordPress排除类别 (Method 1: Exclude a Category from WordPress Using Plugin)
First thing you need to do is to install and activate the 最终类别排除器 plugin. For more details, you should follow our guide on 如何安装 WordPress 插件.
您需要做的第一件事是安装并激活最终类别排除器插件。 有关更多详细信息,您应该按照我们的指南安装 WordPress 插件 。
Upon activation, you’ll need to go to 设置 » 类别排除器 page. It will display all the categories that are available on your WordPress blog.
激活后,您需要转到设置 » 类别排除器页面。 它将显示WordPress博客上可用的所有类别。

From here, you can simply check one or multiple categories under the ‘Exclude from front page’ column. If you want, then you can also exclude the category from RSS订阅, archives, or even from WordPress 搜索.
在这里,您可以简单地检查“从首页排除”列下的一个或多个类别。 如果需要,您还可以将类别从RSS订阅 ,归档甚至WordPress 搜索排除。

Don’t forget to click on the 更新 button to store these settings.
不要忘记单击“ 更新”按钮来存储这些设置。
现在,您可以访问您的网站,查看所选类别的帖子已从首页排除。
您现在可以访问您的网站以查看主页中排除了所选类别的帖子。
方法2:使用代码从WordPress主页中排除类别 (Method 2: Exclude a Category from WordPress Homepage Using Code)
This method requires you to add code to your WordPress files. If you haven’t done this before, then see our guide on how to 在 WordPress 中复制并粘贴代码片段.
此方法要求您将代码添加到WordPress文件中。 如果您以前没有做过,请参阅我们的指南,了解如何在 WordPress 中复制并粘贴代码片段 。
You will need to add following code to your theme’s 函数.php file or a 特定于站点的插件.
您将需要在主题的函数.php文件或具体的于在网站的插件中添加以下代码。
function exclude_category_home( $query ) {
if ( $query->is_home ) {
$query->set( 'cat', '-5' );
}
return $query;
}
add_filter( 'pre_get_posts', 'exclude_category_home' );
不要忘记将 ID (-5) 替换为您的类别 ID。它将隐藏主页中属于与此 ID 匹配的类别的所有博客文章。
不要忘记将 ID(-5) 替换为类别 ID。它将隐藏主页中属于该 ID 类别的所有博客文章。
笔记: Make sure to add a minus (-) sign with the category ID.
注意:确保添加带有类别ID的减号(-)。
Not sure how to find the category ID? See our guide on 如何在 WordPress 中查找类别 ID.
不确定如何找到类别ID? 请参阅我们的指南, 了解如何在 WordPress 中查找类别 ID 。
如果你想排除多个类别,那么你可以像这样更改代码:
如果你想排除多个类别,你可以像这样更改代码:
function exclude_category_home( $query ) {
if ( $query->is_home ) {
$query->set( 'cat', '-5, -9, -23' );
}
return $query;
}
add_filter( 'pre_get_posts', 'exclude_category_home' );
只需将 ID(-5、-9、-23)替换为您的类别 ID,即可将它们从 WordPress 主页中排除。
只需将 ID(-5、-9、-23)替换为您的类别 ID,即可将其从 WordPress 主页中排除。
We hope this article helped you learn how to exclude a category from your WordPress homepage. You may also want to see these 最想要的 WordPress 类别黑客和插件 that will help you use categories more efficiently.
我们希望本文能帮助您学习如何从WordPress主页中排除类别。 您可能还希望看到WordPress这些最想要的类别黑客和插件,这将帮助您更有效地使用类别。
If you liked this article, then please subscribe to our YouTube 频道 for WordPress video tutorials. You can also find us on 推特 and Facebook.
如果您喜欢这篇文章,请订阅我们的YouTube 频道 WordPress视频教程。 您也可以在推特和Facebook上找到我们。
翻译自: https://www.wpbeginner.com/wp-tutorials/how-to-exclude-a-category-from-your-wordpress-homepage/
WordPress 排除类别