wordpress排除分类_如何从WordPress主页中排除类别

wordpress排除分类

Do you want to exclude a specific category from your website’s homepage? By default, WordPress displays posts from all categories on your homepage. In some cases, you may not want that. In this article, we will show you how to easily exclude a category from your WordPress homepage.

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

Exclude Category from WordPress Homepage
为什么要从WordPress主页中排除类别? (Why Exclude a Category from WordPress Homepage?)

WordPress allows you to sort content into categories and tags. Now sometimes you may want to use a category for posts that are not part of your normal blog entries.

WordPress允许您将内容分类为类别和标签。 现在,有时您可能希望对不属于常规博客条目的帖子使用类别。

By default, WordPress does not allow you to hide posts in specific categories from homepage or archives. This leaves you with unwanted posts appearing on your homepage.

默认情况下,WordPress不允许您从首页或档案中隐藏特定类别的帖子。 这样一来,您的首页上就会出现不需要的帖子。

That being said, let’s see how you can easily exclude a specific category from your WordPress homepage. We will show you two different methods, and you can choose the one that best fits your need.

话虽如此,让我们看看如何轻松地从WordPress主页中排除特定类别。 我们将向您展示两种不同的方法,您可以选择最适合您的一种。

方法1:使用插件从WordPress排除类别 (Method 1: Exclude a Category from WordPress Using Plugin)

First thing you need to do is to install and activate the Ultimate Category Excluder plugin. For more details, you should follow our guide on how to install a WordPress plugin.

您需要做的第一件事是安装并激活Ultimate Category Excluder插件。 有关更多详细信息,您应该按照我们的指南安装WordPress插件

Upon activation, you’ll need to go to Settings » Category Excluder page. It will display all the categories that are available on your WordPress blog.

激活后,您需要转到设置»类别排除程序页面。 它将显示WordPress博客上可用的所有类别。

Category excluder settings

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 feeds, archives, or even from WordPress search.

在这里,您可以简单地检查“从首页排除”列下的一个或多个类别。 如果需要,您还可以将类别从RSS提要 ,归档甚至WordPress搜索中排除。

Exclude category

Don’t forget to click on the Update button to store these settings.

不要忘记单击“ 更新”按钮来存储这些设置。

Now you can visit your website to see that the posts from the selected categories are excluded from the front page.

现在,您可以访问您的网站,以查看来自所选类别的帖子被排除在首页之外。

方法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 copy and paste code snippets in WordPress.

此方法要求您将代码添加到WordPress文件中。 如果您以前没有做过,请参阅我们的指南,了解如何在WordPress中复制和粘贴代码段

You will need to add following code to your theme’s functions.php file or a site-specific plugin.

您将需要在主题的functions.php文件或特定站点的插件中添加以下代码。



function exclude_category_home( $query ) {
if ( $query->is_home ) {
$query->set( 'cat', '-5' );
}
return $query;
}

add_filter( 'pre_get_posts', 'exclude_category_home' );


Don’t forget to replace ID (-5) with your category ID. It will hide all blog posts from homepage belonging to the category that matches this ID.

不要忘记用类别ID替换ID(-5)。 它将隐藏主页中属于该ID类别的所有博客文章。

Note: Make sure to add a minus (-) sign with the category ID.

注意:确保添加带有类别ID的减号(-)。

Not sure how to find the category ID? See our guide on how to find category IDs in WordPress.

不确定如何找到类别ID? 请参阅我们的指南, 了解如何在WordPress中查找类别ID

If you want to exclude multiple categories, then you can change the code like this:

如果要排除多个类别,则可以像这样更改代码:



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' );


Simply replace the IDs (-5, -9, -23) with your category IDs to exclude them from your WordPress homepage.

只需将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 most wanted category hacks and plugins for WordPress that will help you use categories more efficiently.

我们希望本文能帮助您学习如何从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-a-category-from-your-wordpress-homepage/

wordpress排除分类

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值