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

WordPress带有内置的默认RSS源。您可以通过向RSS源添加自定义内容来调整默认源,甚至可以将RSS缩略图添加到RSS源中。对于大多数用户来说,默认的RSS和Atom供稿已足够,但您可能希望创建自定义RSS供稿以提供特定类型的内容。在本文中,我们将向您展示如何在WordPress中创建自定义RSS源。

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

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

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

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

  • Title
  • Link
  • Published Date
  • 作者
  • 摘录

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

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

上面的代码触发了 customRSS 函数,用于添加Feed。add_feed函数有两个参数,feedname和回调函数。feedname将组成您的新Feed url yourdomain.com/feed/feedname,并且将调用回调函数来实际创建Feed。记下feedname,稍后您将需要它。

初始化Feed后,您需要使用以下代码创建回调函数以生成所需的Feed。theme的 functions.php 文件或特定于站点的插件:

  function customRSSFunc(){          get_template_part("rss", "feedname");  }  

上面的代码使用 get_template_part 函数要链接到单独的模板文件,您也可以将RSS代码直接放入函数中。通过使用 get_template_part ,我们可以将功能与布局分开。get_template_part function有两个参数,slug和name,它们将查找一个模板文件,其名称采用以下格式,从顶部的文件开始(如果找不到第一个,它将转到第二个,依此类推):

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

出于本教程的目的,最好是将slug设置为您正在创建的Feed类型(在本例中为:rss),以及之前配置的feedname的名称。

一旦您告诉WordPress查找Feed模板,我需要创造它。下面的代码将使用我们之前列出的信息生成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>  

此模板代码将生成一个RSS源,以上布局。 postCount 变量允许您控制要在Feed中显示的帖子数量。模板可以根据需要进行修改,以显示您需要的任何信息(例如发布图像,评论等)。

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

最后,要显示您的Feed,您首先需要刷新WordPress重写规则。最简单的方法是登录WordPress管理员,然后单击 Settings – &gt;固定链接。在这里,只需点击保存更改,这将刷新重写规则。

您现在可以在 yourdomain.com上访问新的Feed了/ feed / feedname ,其中feedname是您之前在 add_feed 函数中提供的源名称。

W3C提供了Feed验证服务,允许您验证生成的Feed。

疑难解答

  • 我在尝试查看我的Feed时遇到404错误!
    • 检查您是否在网址中使用了正确的Feedname。它必须是您在 add_feed 函数中提供的函数
    • 如果您具有正确的feedname,则您的重写规则可能无法正确刷新。重新保存固定链接只是为了确保。
    • 如果你已经重新保存永久链接,你可以通过主题的functions.php文件强制重写刷新。将以下代码添加到我们之前创建的customRSS函数中。确保在 add_feed 函数之后添加代码。
      global $wp_rewrite;  $wp_rewrite->flush_rules();  
  • 一旦添加了此代码,请重新加载WordPress站点。注意:应在使用后立即将其删除。一次就足以刷新规则。
  • 我的Feed没有验证!
    • 使用W3C Feed验证程序,应该在您的Feed未验证的位置给出具体的详细信息。编辑Feed模板文件以解决这些问题
  • 我得到一个&lt; language /&gt;验证错误!
    • 这在您的WordPress安装中尚未配置RSS语言时很常见。为此,您可以将以下代码添加到主题的 functions.php 文件中,以更新语言选项。
      function rssLanguage(){          update_option("rss_language", "en");  }  add_action("admin_init", "rssLanguage");  
  • 编辑update_option函数的第二个参数将语言更改为您需要的语言。查看RSS语言代码的完整列表。
  • 将上述代码添加到您的函数文件后,加载WordPress管理界面以使其生效。此后,应从WordPress函数文件中删除代码。加载一次足以配置rss_language设置。
  • 这也可以直接在数据库中完成,方法是查找wp_options表中的rss_language选项。
  • WordPress带有内置的默认RSS源。您可以通过向RSS源添加自定义内容来调整默认源,甚至可以将RSS缩略图添加到RSS源中。对于大多数用户来说,默认的RSS和Atom供稿已足够,但您可能希望创建自定义RSS供稿以提供特定类型的内容。在本文中,我们将向您展示如何在WordPress中创建自定义RSS源。

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

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

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

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

    • Title
    • Link
    • Published Date
    • 作者
    • 摘录

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

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

    上面的代码触发了 customRSS 函数,用于添加Feed。add_feed函数有两个参数,feedname和回调函数。feedname将组成您的新Feed url yourdomain.com/feed/feedname,并且将调用回调函数来实际创建Feed。记下feedname,稍后您将需要它。

    初始化Feed后,您需要使用以下代码创建回调函数以生成所需的Feed。theme的 functions.php 文件或特定于站点的插件:

      function customRSSFunc(){          get_template_part("rss", "feedname");  }  

    上面的代码使用 get_template_part 函数要链接到单独的模板文件,您也可以将RSS代码直接放入函数中。通过使用 get_template_part ,我们可以将功能与布局分开。get_template_part function有两个参数,slug和name,它们将查找一个模板文件,其名称采用以下格式,从顶部的文件开始(如果找不到第一个,它将转到第二个,依此类推):

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

    出于本教程的目的,最好是将slug设置为您正在创建的Feed类型(在本例中为:rss),以及之前配置的feedname的名称。

    一旦您告诉WordPress查找Feed模板,我需要创造它。下面的代码将使用我们之前列出的信息生成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>  

    此模板代码将生成一个RSS源,以上布局。 postCount 变量允许您控制要在Feed中显示的帖子数量。模板可以根据需要进行修改,以显示您需要的任何信息(例如发布图像,评论等)。

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

    最后,要显示您的Feed,您首先需要刷新WordPress重写规则。最简单的方法是登录WordPress管理员,然后单击 Settings – &gt;固定链接。在这里,只需点击保存更改,这将刷新重写规则。

    您现在可以在 yourdomain.com上访问新的Feed了/ feed / feedname ,其中feedname是您之前在 add_feed 函数中提供的源名称。

    W3C提供了Feed验证服务,允许您验证生成的Feed。

    疑难解答

    • 我在尝试查看我的Feed时遇到404错误!
      • 检查您是否在网址中使用了正确的Feedname。它必须是您在 add_feed 函数中提供的函数
      • 如果您具有正确的feedname,则您的重写规则可能无法正确刷新。重新保存固定链接只是为了确保。
      • 如果你已经重新保存永久链接,你可以通过主题的functions.php文件强制重写刷新。将以下代码添加到我们之前创建的customRSS函数中。确保在 add_feed 函数之后添加代码。
        global $wp_rewrite;  $wp_rewrite->flush_rules();  
    • 一旦添加了此代码,请重新加载WordPress站点。注意:应在使用后立即将其删除。一次就足以刷新规则。
  • 我的Feed没有验证!
    • 使用W3C Feed验证程序,应该在您的Feed未验证的位置给出具体的详细信息。编辑Feed模板文件以解决这些问题
  • 我得到一个&lt; language /&gt;验证错误!
    • 这在您的WordPress安装中尚未配置RSS语言时很常见。为此,您可以将以下代码添加到主题的 functions.php 文件中,以更新语言选项。
      function rssLanguage(){          update_option("rss_language", "en");  }  add_action("admin_init", "rssLanguage");  
  • 编辑update_option函数的第二个参数将语言更改为您需要的语言。查看RSS语言代码的完整列表。
  • 将上述代码添加到您的函数文件后,加载WordPress管理界面以使其生效。此后,应从WordPress函数文件中删除代码。加载一次足以配置rss_language设置。
  • 这也可以直接在数据库中完成,方法是查找wp_options表中的rss_language选项。
  • 相关推荐

    发表回复

    房先生
    我们将24小时内回复。
    2024-05-10 12:49:58
    您好,有任何疑问请与我们联系!
    您的工单我们已经收到,我们将会尽快跟您联系!
    [QQ客服]
    2781198
    加我微信
    [电话联系]
    13585372227
    [电子邮件]
    chaoneo@163.com
    取消

    选择聊天工具: