wordpress 自定义_如何在WordPress中创建自定义RSS源

wordpress 自定义

WordPress comes with built-in default RSS feeds. You can tweak the default feeds by adding custom content to your RSS Feeds, or even adding post thumbnail to your RSS Feeds. 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 feed 。 您可以通过向RSS Feed中添加自定义内容 ,甚至 RSS Feed中 添加帖子缩略图来调整默认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 local install.

请注意,本教程不适用于初学者WordPress用户。 如果您是初学者,但仍想尝试,请在本地安装上进行

As always, you must create a complete backup of your WordPress website before making any major changes to a live website.

与往常一样,您必须对WordPress网站创建完整的备份,然后再对实时网站进行任何重大更改。

Having said that, let’s get started with your first custom RSS feed in WordPress.

话虽如此,让我们开始使用WordPress中的第一个自定义RSS feed。

Let’s assume you want to create a new RSS feed which displays just the following information:

假设您要创建一个新的RSS feed,它仅显示以下信息:

  • 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 site-specific plugin:

您需要做的第一件事是在主题的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,它们将以以下格式查找名称为以下格式的模板文件,从顶部的文件开始(如果找不到第一个文件,则将移至第二个文件) , 等等):

  1. wp-content/themes/child/rss-feedname.phpwp-content/themes/child/rss-feedname.php
  2. wp-content/themes/parent/rss-feedname.phpwp-content/themes/parent/rss-feedname.php
  3. wp-content/themes/child/rss.phpwp-content/themes/child/rss.php
  4. wp-content/themes/parent/rss.phpwp-content/themes/parent/rss.php

For the purposes of this tutorial, it is best to set the slug to the type of feed you’re creating (in this case: rss), and the name to the feedname configured earlier on.

为了本教程的目的,最好将slug设置为要创建的feed的类型(在本例中为rss),并将名称设置为之前配置的feedname。

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 Settings -> Permalinks. Once here, just click Save Changes, 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 feed validation service, allowing you to validate the resulting feed.

W3C提供了提要验证服务 ,使您可以验证生成的提要。

故障排除 (Troubleshooting)
  • I’m getting a 404 error when trying to view my 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. NOTE: This should be removed immediately after use. Once is enough for the rules to be flushed.
  • 添加完毕后,请重新加载WordPress网站。 注意:使用后应立即将其删除。 一次足以刷新规则。
  • My feed isn’t validating! 我的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提要验证器,应在不验证提要的地方给出特定的详细信息。 编辑提要模板文件以解决这些问题
  • I’m getting a <language /> validation error! 我收到<language />验证错误!
    • functions.php file, to update the language option.functions.php文件中,以更新语言选项。
    
    function rssLanguage(){
            update_option('rss_language', 'en');
    }
    add_action('admin_init', 'rssLanguage');
    
    
  • RSS Language Codes.RSS语言代码的完整列表。
  • Once the above code has been added to your functions file, load the WordPress admin screen for it to take effect. After this, the code should be removed from your WordPress functions file. Loading it once is enough to configure the rss_language setting.
  • 将以上代码添加到函数文件后,请加载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选项,也可以直接在数据库中完成此操作。
  • We hope this article helped you create your own custom RSS Feeds in WordPress. Let us know how and why you will be using custom RSS feeds on your WordPress site by leaving a comment below.

    我们希望本文能帮助您在WordPress中创建自己的自定义RSS Feed。 通过在下面留下评论,让我们知道如何以及为什么将在WordPress网站上使用自定义RSS feed。

    翻译自: https://www.wpbeginner.com/wp-tutorials/how-to-create-custom-rss-feeds-in-wordpress/

    wordpress 自定义

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值