
WordPress 定制
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.
WordPress带有内置的默认RSS订阅 。 您可以通过将自定义内容添加到您的 RSS 源 ,甚至向 RSS订阅 添加帖子缩略图到调整默认Feed。 对于大多数用户而言,默认的RSS和Atom提要就足够了,但是您可能希望创建一个自定义RSS提要来传递特定类型的内容。 在本文中,我们将向您展示如何在WordPress中创建自定义RSS feed。
Please note that this tutorial is not intended for beginner level WordPress users. If you are a beginner, and still want to try it, then please do so on a 本地安装.
请注意,本教程不适用于初学者WordPress用户。 如果您是初学者,但仍想尝试,请在本地安装 。
As always, you must create a 完整备份您的 WordPress 网站 before making any major changes to a live website.
与往常一样,您必须对于 WordPress 网站创建完整备份,然后再对实时网站进行任何重大更改。
话虽如此,让我们开始在 WordPress 中创建第一个自定义 RSS 源。
话虽如此,让我们开始在 WordPress 中创建第一个自定义 RSS 源。
假设您要创建一个新的 RSS 源,仅显示以下信息:
假设您想要创建一个仅显示以下信息的新 RSS 源:
- Title 标题
- Link 链接
- Published Date 发布日期
- Author 作者
- Excerpt 摘抄
First thing you need to do is create the new RSS feed in your theme’s functions.php
file or in a 特定于站点的插件:
您需要做的第一件事是在主题的functions.php
文件或具体的于在网站的插件中创建新的RSS feed:
add_action('init', 'customRSS');
function customRSS(){
add_feed('feedname', 'customRSSFunc');
}
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.
上面的代码触发了customRSS
函数,该函数添加了提要。 add_feed函数有两个参数,feedname和一个回调函数。 提要名称将构成您的新提要URL yourdomain.com/feed/feedname
并且将调用回调函数来实际创建提要。 记下提要名称,因为稍后将需要它。
Once you have initialized the feed, you’ll need to create the callback function to produce the required feed, using the following code in your theme’s functions.php
file or in a site specific plugin:
提要初始化之后,您需要使用主题主题的functions.php
文件或特定于站点的插件中的以下代码,创建回调函数以生成所需的提要:
function customRSSFunc(){
get_template_part('rss', 'feedname');
}
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):
上面的代码使用get_template_part
函数链接到单独的模板文件,但是您也可以将RSS代码直接放入函数中。 通过使用get_template_part
,我们可以使功能与布局分开。 get_template_part
函数有两个参数slug和name,它们将以以下格式查找名称为以下格式的模板文件,从顶部的文件开始(如果找不到第一个文件,则将移至第二个文件) , 等等):
wp-content/themes/child/rss-feedname.php
wp-content/themes/child/rss-feedname.php
wp-content/themes/parent/rss-feedname.php
wp-content/themes/parent/rss-feedname.php
wp-content/themes/child/rss.php
wp-content/themes/child/rss.php
wp-content/themes/parent/rss.php
wp-content/themes/parent/rss.php
出于本教程的目的,最好将 slug 设置为您要创建的提要类型(在本例中为:rss),并将名称设置为之前配置的提要名称。
出于本教程的目的,最好将 slug 设置为您要创建的提要类型(在本例中为 rss),并将名称设置为您之前配置的提要名称。
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.
告诉WordPress查找提要模板后,您需要创建它。 以下代码将使用我们之前列出的信息来生成Feed的布局。 将此文件保存为主题文件夹中的文件,作为在get_template_part
函数中配置的slug-name.php模板文件。
<?php
/**
* Template Name: Custom RSS Template - Feedname
*/
$postCount = 5; // The number of posts to show in the feed
$posts = query_posts('showposts=' . $postCount);
header('Content-Type: '.feed_content_type('rss-http').'; charset='.get_option('blog_charset'), true);
echo '<?xml version="1.0" encoding="'.get_option('blog_charset').'"?'.'>';
?>
<rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
<?php do_action('rss2_ns'); ?>>
<channel>
<title><?php bloginfo_rss('name'); ?> - Feed</title>
<atom:link href="<?php self_link(); ?>" rel="self" type="application/rss+xml" />
<link><?php bloginfo_rss('url') ?></link>
<description><?php bloginfo_rss('description') ?></description>
<lastBuildDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_lastpostmodified('GMT'), false); ?></lastBuildDate>
<language><?php echo get_option('rss_language'); ?></language>
<sy:updatePeriod><?php echo apply_filters( 'rss_update_period', 'hourly' ); ?></sy:updatePeriod>
<sy:updateFrequency><?php echo apply_filters( 'rss_update_frequency', '1' ); ?></sy:updateFrequency>
<?php do_action('rss2_head'); ?>
<?php while(have_posts()) : the_post(); ?>
<item>
<title><?php the_title_rss(); ?></title>
<link><?php the_permalink_rss(); ?></link>
<pubDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_post_time('Y-m-d H:i:s', true), false); ?></pubDate>
<dc:creator><?php the_author(); ?></dc:creator>
<guid isPermaLink="false"><?php the_guid(); ?></guid>
<description><![CDATA[<?php the_excerpt_rss() ?>]]></description>
<content:encoded><![CDATA[<?php the_excerpt_rss() ?>]]></content:encoded>
<?php rss_enclosure(); ?>
<?php do_action('rss2_item'); ?>
</item>
<?php endwhile; ?>
</channel>
</rss>
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).
此模板代码将按照上述布局生成RSS源。 postCount
变量使您可以控制要在Feed中显示的帖子数。 可以根据需要修改模板,以显示所需的任何信息(例如,帖子图像,评论等)。
The the_excerpt_rss
function will display the excerpt of each post, and for posts that do not have excerpts, it will display the first 120 words of the post’s content.
the_excerpt_rss
函数将显示每个帖子的摘录,对于没有摘要的帖子,它将显示帖子内容的前120个单词。
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.
最后,要显示您的供稿,您首先需要刷新WordPress重写规则。 最简单的方法是登录WordPress管理员,然后单击设置->固定链接 。 进入此处后,只需点击保存更改 ,这将刷新重写规则。
You can now access your new feed at yourdomain.com/feed/feedname
, where feedname was the feedname you gave in the add_feed
function earlier on.
现在,您可以在yourdomain.com/feed/feedname
访问新的提要,其中提要名是您先前在add_feed
函数中指定的add_feed
。
The W3C offers a 饲料验证服务, allowing you to validate the resulting feed.
W3C提供了饲料验证服务 ,使您可以验证生成的提要。
故障排除 (Troubleshooting)
- 我在尝试查看我的 Feed 时收到 404 错误! 我在尝试查看 Feed 时收到 404 错误!
add_feed
functionadd_feed
函数中提供的- If you have the correct feedname, your rewrite rules may not have flushed correctly. Re-save your permalinks just to be sure. 如果您使用正确的Feed名称,则您的重写规则可能未正确刷新。 请务必重新保存您的永久链接。
add_feed
function.add_feed
函数之后添加代码。
global $wp_rewrite; $wp_rewrite->flush_rules();
- Once you’ve added this, reload your WordPress site. 注意:使用后应立即除去。一次就足以刷新规则。
- 添加完毕后,请重新加载WordPress网站。 注意:使用后应立即将其取下。 刷新规则一次就够了。
- 我的 Feed 未验证! 我的 Feed 无法验证!
- Using the W3C feed validator, specific details should be given where your feed isn’t validating. Edit the feed template file to resolve these issues 使用W3C提要验证器,应在不验证提要的地方给出特定的详细信息。 编辑提要模板文件以解决这些问题
- 我收到 <language /> 验证错误! 我收到 <language /> 验证错误!
functions.php
file, to update the language option.functions.php
文件中,以更新语言选项。
function rssLanguage(){ update_option('rss_language', 'en'); } add_action('admin_init', 'rssLanguage');
- RSS 语言代码.RSS 语言代码的完整列表。
- Once the above code has been added to your functions file, load the WordPress admin screen for it to take effect. 此后,代码应该从您的 WordPress 函数文件中删除。加载一次就足以配置 rss_language 设置。
- 将以上代码添加到函数文件后,请加载WordPress管理屏幕以使其生效。 之后,应从 WordPress 函数文件中删除该代码。 加载一次就足以配置 rss_language 设置。
- This can also be done directly in the database, by looking for the rss_language option in the wp_options table. 通过在wp_options表中查找rss_language选项,也可以直接在数据库中完成此操作。
我们希望本文能帮助您在 WordPress 中创建自己的自定义 RSS 源。请在下面留下评论,让我们知道您将如何以及为何在 WordPress 网站上使用自定义 RSS 源。
我们希望本文能帮助您在 WordPress 中创建自己的自定义 RSS 源。请在下面留下评论,让我们知道您如何以及为何在 WordPress 网站上使用自定义 RSS 源。
翻译自: https://www.wpbeginner.com/wp-tutorials/how-to-create-custom-rss-feeds-in-wordpress/
WordPress 定制