防止类别中的帖子显示在WordPress的RSS Feed中

I recently posted a snippet which prevented posts in a given category from displaying in WordPress' main loop.  I even went on to create my first WordPress plugin to accomplish the task via the WordPress control panel.  The one task those snippets doesn't complete is preventing posts from going into RSS feeds.  Here's how you can prevent posts in a category from displaying in the RSS feed!

我最近发布了一个代码段,该代码段阻止了给定类别的帖子显示在WordPress的主循环中 。 我什至继续创建我的第一个WordPress插件,以通过WordPress控制面板完成任务。 这些摘要未完成的一项任务是阻止帖子进入RSS feed。 这是您如何防止类别中的帖子显示在RSS feed中的方法!

PHP (The PHP)

The category check is done the same way the loop check was done:

完成类别检查的方式与执行循环检查的方式相同:


function postsFilter($query) {
    // Prevent from RSS feed
    if($query->is_feed()) {
    	// No posts in category #11 may go into the feed
    	$query->set('cat', '-11');
    }
}
add_action('pre_get_posts', 'postsFilter');


A pre_get_posts filter is added to check for the is_feed() return value.  If the value is true, we add the condition to remove the category form the final query.  I have added this functionality to the WordPress plugin as well!

添加了pre_get_posts过滤器以检查is_feed()返回值。 如果值为true,则添加条件以从最终查询中删除类别。 我也已将此功能添加到WordPress插件中!

翻译自: https://davidwalsh.name/prevent-rss

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值