联系我们
简单又实用的WordPress网站制作教学
当前位置:网站首页 > WordPress教程 > 正文

如何在WordPress中创建自定义RSS

作者:98wpeu发布时间:2024-01-31分类:WordPress教程浏览:74


导读:WordPress带来了内置的默认RSS提示。Youcan通过向RSS提出添加自定义内容,甚至向RSS提出添加种子缩略图来调整种子默认提交。Ifyouwanttorece...

WordPress带来了内置的默认RSS提示。You can通过向RSS提出添加自定义内容,甚至向RSS提出添加种子缩略图来调整种子默认提交。If you want to receive a message,you must receive a message.在本文中,我们将向您展示如何在WordPress中创建自定义RSS提示。

请注意,本教程不是针对初学者的WordPress用户。如果您是初学者,并且仍然想尝试它,那么请在本地安装。

As usual,在对实时网站进行任何重大修改之前,您必须创建WordPress网站的完整备份.

话虽如此,让我们开始使用您在WordPress中的第一个定制RSS吧。

If you want to create a new RSS promise,it only show the following information:

标题 链接 发布日期 作者 摘录

您需要做的第一件事是在主题的functions.php文件或特定于站点的插件:


add_action('init', 'customRSS');
function customRSS(){
        add_feed('feedname', 'customRSSFunc');
}

由❤️托管WPCode
在WordPress中一键使用

上面的代码触发customRSS函数,该函数添加提要。ADD_FEED函数有两个参数:feed name和一个回调函数。(Information technology-Open Systems Interconnection-Part 2:Interconnection protocol)yourdomain.com/feed/feedname并且将调用回调函数来实际创建提要。Remember the name of Feed,because you will use it after you.

初始化提要后,您将需要创建回调函数来生成所需的提要,在主题的functions.php文件或站点特定插件中:

function customRSSFunc(){
        get_template_part('rss', 'feedname');
}
由❤️托管WPCode
在WordPress中一键使用

上面的代码使用get_template_partThe results showed that the results showed that there was no significant difference between the two groups(P> 0.05).通过使用get_template_part,我们可以将功能与布局分开。这个get_template_partFunctions have two parameters,slug and name,these two parameters will find name for以下格式的模板文件,from top of the file start(if找不到第一个文件,则转到第二个文件,依据此条):

wp-content/themes/child/rss-feedname.phpwp-content/themes/parent/rss-feedname.phpwp-content/themes/child/rss.phpwp-content/themes/parent/rss.php

出于本教程的目的,最好将slug设置为您正在创建的提示类型(在本示例中为:RSS),并将名称设置为前面配置的提示名称。

一旦你告诉WordPress寻找提示模板,你就需要创建它。下面的代码将使用我们前面列出的信息生成提要的布局。将此文件另存为配置在主题文件夹中的slug-name.php模板文件get_template_part功能。

<?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>
由❤️托管WPCode
在WordPress中一键使用

The template code will generate a RSS promise with the above layout.这个postCount变量允许您控制提要中显示的帖子数量。模板可以根据需要进行修改,以显示您需要的任何信息(例如,发布图像,评论等)。

这个the_excerpt_rss函数将显示每个帖子的摘录,对于没有摘录的帖子,它将显示帖子内容的前120个单词。

最后,要显示提示,首先需要更新WordPress重写规则。最简单的方法是登录到WordPress管理员,然后点击Set-gt; Fixed Link。进入此处后,只需点击保存更改,这将刷新重写规则。

您现在可以通过以下地址访问您的新订阅源yourdomain.com/feed/feedname,其中feed name是您在add_feed之前的功能。

The results showed that the results of the study were significantly higher than that of the control group(P <0.05).

故障排除

I'm sorry. I'm sorry. I'm sorry.Check if you used the correct name in the URL.它必须是您在add_feed功能 如果您有正确的提要名称,则您的重写规则可能没有正确刷新。重新保存您的固定链接只是为了确保。 If you have restored the fixed link,you can make rewrite new through your subject's function.php file. The following code adds to our previous created stomRSS function.确保将代码添加到add_feed功能。
global $wp_rewrite;
$wp_rewrite->flush_rules();
由❤️托管WPCode
在WordPress中一键使用 一旦你添加了这个,重新加载你的WordPress插件。注意:使用后应立即将其取出。一次足够让规则被冲走。My subscription source cannot be validated!In this paper,we study the relationship between the factors of the influence of the编辑提要模板文件以解决这些问题I received language/gt; validation error!在您的WordPress安装中没有配置RSS语言的情况下,这种情况很常见。为此,您可以将以下代码添加到主题的functions.php文件,以更新语言选项。
function rssLanguage(){
        update_option('rss_language', 'en');
}
add_action('admin_init', 'rssLanguage');
由❤️托管WPCode
在WordPress中一键使用 编辑UPDATE_OPTION函数的第二个参数,将语言更改为您需要的语言。查看RSS语言代码的完整列表。 When将上述代码添加到您的函数文件中,请加载WordPress管理屏幕以使其生效。在此之后,代码应该从您的WordPress函数文件中删除。添加一次就足以配置RSS_LANGUAGE设置。这也可以通过在wp_options表中查找RSS_LANGUAGE选项直接在数据库中完成。

We hope this article can help you create your own customization RSS in WordPress.在下面留下评论,让我们知道你将如何以及为什么在你的WordPress网站上使用定制的RSS。

标签:自定义提要如何在WordPressRSS


WordPress教程排行
最近发表
网站分类
标签列表