如何将自定义帖子类型添加到您的主要WordPress RSS Feed

Custom Post Types was one of the most hyped feature of WordPress 3.0. This feature alone expanded the horizon of WordPress usage as a Content Management System (CMS). If you are using Custom Post Types, or thinking of custom post types, then you may have the urge to add it into your main RSS Feed. This option is not built-in by default because your main WordPress RSS feed only includes “Posts” not even pages, so custom post types are a long shot. In this article, we will share how you can add Custom Post Types to your main WordPress RSS feeds.

自定义帖子类型是WordPress 3.0的最受吹捧的功能之一。 仅此功能就扩展了WordPress作为内容管理系统(CMS)的使用范围。 如果您正在使用“自定义帖子类型”,或者正在考虑自定义帖子类型,那么您可能会希望将其添加到主RSS Feed中。 默认情况下,此选项不是内置的,因为您的主要WordPress RSS feed仅包含“帖子”,甚至不包括页面,因此自定义帖子类型很长。 在本文中,我们将分享如何将自定义帖子类型添加到您的主要WordPress RSS feed中。

You would need to open your theme’s functions.php file and add the following code within the PHP markup:

您将需要打开主题的functions.php文件,并在PHP标记内添加以下代码:

function myfeed_request($qv) {
	if (isset($qv['feed']))
		$qv['post_type'] = get_post_types();
	return $qv;
}
add_filter('request', 'myfeed_request');

This code modifies the query to keep the default content type for blog posts “post” in the main RSS feed, and also adding new custom post types.

此代码修改了查询,以在主RSS feed中将博客帖子的默认内容类型保留为“ post”,并且还添加了新的自定义帖子类型。

But what if you have five custom post types in your new project, and you only want to add three to the main RSS feed? Well that shouldn’t be any problem because we will just modify the code slightly to give you the option to only include the ones that you want.

但是,如果您在新项目中有五个自定义帖子类型,并且只想在主RSS feed中添加三个,该怎么办? 嗯,那应该没什么问题,因为我们只需要稍微修改一下代码即可为您提供只包含所需代码的选项。

function myfeed_request($qv) {
	if (isset($qv['feed']) && !isset($qv['post_type']))
		$qv['post_type'] = array('post', 'story', 'books', 'movies');
	return $qv;
}
add_filter('request', 'myfeed_request');

If you see in the code above, we simply added an array to modify the post_types that will be shown in the main RSS feed. We are showing the default posts, story, books, and movies.

如果您在上面的代码中看到,我们只需添加一个数组即可修改将在主RSS提要中显示的post_types。 我们正在显示默认的帖子,故事,书籍和电影。

Source: Core Trac Ticket #12943

资料来源: Core Trac门票#12943

翻译自: https://www.wpbeginner.com/wp-tutorials/how-to-add-custom-post-types-to-your-main-wordpress-rss-feed/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值