高级管理文档

WordPress 订阅源

💡 云策文档标注

概述

本文档介绍了 WordPress 内置的订阅源功能,包括如何访问不同类型的订阅源、在主题中添加订阅源链接以及自定义订阅源地址。

关键要点

  • WordPress 默认提供多种订阅源类型,如 RDF/RSS 1.0、RSS 0.92、RSS 2.0、Atom 和评论 RSS 2.0,可通过 bloginfo() 模板标签获取 URL。
  • 订阅源通常显示在主题的侧边栏或页脚中,用于展示网站内容更新或评论。
  • 可以使用 post_comments_feed_link() 模板标签为特定文章添加评论订阅源。
  • 在主题中添加订阅源链接时,需编辑 sidebar.php 或 footer.php 文件,并插入相应的模板标签。
  • 可以为订阅源链接添加图形按钮,通过将链接包裹在 标签中实现。
  • 迁移网站或更换博客软件时,可通过 .htaccess 文件重写规则将旧订阅源地址重定向到新的 WordPress 订阅源。

代码示例

// 获取 RSS 2.0 订阅源 URL
<?php bloginfo('rss2_url'); ?>

// 添加评论订阅源链接
<?php post_comments_feed_link('RSS 2.0'); ?>

// 在主题中添加订阅源链接列表
<ul class="feeds">
  <li><a href="<?php bloginfo('rss2_url'); ?>" title="<?php _e('Syndicate this site using RSS'); ?>"><?php _e('<abbr title="Really Simple Syndication">RSS</abbr>'); ?></a></li>
  <li><a href="<?php bloginfo('atom_url'); ?>" title="<?php _e('Syndicate this site using Atom'); ?>"><?php _e('Atom'); ?></a></li>
  <li><a href="<?php bloginfo('comments_rss2_url'); ?>" title="<?php _e('The latest comments to all posts in RSS'); ?>"><?php _e('Comments <abbr title="Really Simple Syndication">RSS</abbr>'); ?></a></li>
</ul>

// 为订阅源链接添加图形按钮
<a href="<?php bloginfo('rss2_url'); ?>" title="<?php _e('Syndicate this site using RSS'); ?>"><img src="https://example.com/images/feed-icon-14x14.png" alt="RSS Feed" title="RSS Feed"></a>

// .htaccess 重写规则示例(从 b2 迁移)
RewriteRule ^b2rss2.php(.*)? /wordpress/?feed=rss2 [QSA]

注意事项

  • 并非所有主题都包含所有订阅源类型,可能需要手动添加。
  • 自定义订阅源功能(如修改内容)需参考相关文档,本文档仅覆盖基础操作。
  • 使用 .htaccess 重定向时,确保文件存在并正确配置,以避免网站错误。

📄 原文内容

WordPress Built-in Feeds

By default, WordPress comes with various feeds. They are generated by template tag for bloginfo() for each type of feed and are typically listed in the sidebar and/or footer of most WordPress Themes. They look like this:

URL for RDF/RSS 1.0 feed

<?php bloginfo('rdf_url'); ?>

URL for RSS 0.92 feed

<?php bloginfo('rss_url'); ?>

URL for RSS 2.0 feed

<?php bloginfo('rss2_url'); ?>

URL for Atom feed

<?php bloginfo('atom_url'); ?>

URL for comments RSS 2.0 feed

<?php bloginfo('comments_rss2_url'); ?>

The first four feeds display recent updates and changes to your site’s content for the different feedreaders. Of these, the RSS feeds are the most well known. The last feed example is used by RSS 2.0 feedreaders and does not show your site’s content. It only shows the comments made on your site.

To track the comments on a specific post, the post_comments_feed_link() template tag is used on single post pages like this:

<?php post_comments_feed_link('RSS 2.0'); ?>

There are ways to modify these feeds, and these are covered in the article on Customizing Feeds.

Adding Feeds

Not all WordPress Themes feature all of the RSS Feed types that are available through WordPress. To add a feed to your site, find the location of where the other feeds are, typically in your sidebar.php or footer.php template files of your Theme. Then add one of the tags listed above to the list, like this example:

<ul class="feeds">
  <li><a href="<?php bloginfo('rss2_url'); ?>" title="<?php _e('Syndicate this site using RSS'); ?>"><?php _e('<abbr title="Really Simple Syndication">RSS</abbr>'); ?></a></li>
  <li><a href="<?php bloginfo('atom_url'); ?>" title="<?php _e('Syndicate this site using Atom'); ?>"><?php _e('Atom'); ?></a></li>
  <li><a href="<?php bloginfo('comments_rss2_url'); ?>" title="<?php _e('The latest comments to all posts in RSS'); ?>"><?php _e('Comments <abbr title="Really Simple Syndication">RSS</abbr>'); ?></a></li>
</ul>

Many people like to have a graphic representing the feed instead of words. There are now standards for these graphics or “buttons”, but you can make your own to match the look and colors on your site.

To add a graphic to your feed link, simply wrap the link around the graphic such as:

<a href="<?php bloginfo('rss2_url'); ?>" title="<?php _e('Syndicate this site using RSS'); ?>"><img src="https://example.com/images/feed-icon-14x14.png" alt="RSS Feed" title="RSS Feed"></a>

Changing Addresses

If you are currently using other webblog software and are changing to WordPress, or are moving your weblog to a new location, you can “forward” RSS readers to your new RSS feeds using file rewrites and redirects in your .htaccess file.

Edit the .htaccess file in your root folder; if no file exists, create one.

Here is an example for a b2 feed:

RewriteRule ^b2rss2.php(.*)? /wordpress/?feed=rss2 [QSA]

Here is an example for MovableType Users:

RewriteRule ^index.xml(.*)? /wordpress/?feed=rss2 [QSA]