WordPress comes with built-in default RSS订阅. You can tweak the default feeds by 将自定义内容添加到您的 RSS 源, or even 将帖子缩略图添加到您的 RSS 源. The default RSS and Atom feeds are enough for most users, but you may wish to create a custom RSS feed for delivering specific type of content. In this article, we will show you how to create custom RSS feeds in WordPress.
The above code triggers the customRSS function, which adds the feed. The add_feed function has two arguments, feedname, and a callback function. The feedname will make up your new feed url yourdomain.com/feed/feedname and the callback function will be called to actually create the feed. Make a note of the feedname, as you’ll need this later on.
The code above is using the get_template_part function to link to a separate template file, however you can also place the RSS code directly into the function. By using get_template_part, we can keep the functionality separate to the layout. The get_template_part function has two arguments, slug and name, that will look for a template file with the name in the following format, starting with the file at the top (if it doesn’t find the first, it will move on to the second, and so on):
Once you’ve told WordPress to look for the feed template, you’ll need to create it. The below code will produce the layout for the feed with the information we listed earlier. Save this file in your theme folder as the slug-name.php template file configured in the get_template_part function.
This template code will generate an RSS feed following the above layout. The postCount variable allows you to control the number of posts to display in your feed. The template can be amended as required to display whatever information you require (e.g. post images, comments, etc).
Finally, to display your feed, you’ll first need to flush your WordPress rewrite rules. The easiest way to do this is by logging in to the WordPress admin, and clicking 设置 -> 固定链接. Once here, just click 保存更改, which will flush the rewrite rules.