通过 RSS 在 WordPress 中联合自定义字段

发布于 2024-12-03 04:43:40 字数 770 浏览 2 评论 0原文

我想知道我是否可以问一个 WordPress / RSS 问题,我在这里找不到答案,

尝试使用 FeedWordpress 插件作为 RSS 聚合器,通过 WordPress 中的 RSS 联合帖子,原始博客中的每个帖子都包含五个自定义字段,对其主题功能很重要(使用相同主题的原始博客和联合/接收博客)。

原始 RSS2 提要不包含这些自定义字段,除了一个在默认 rss 提要模板中定义的附件字段(WP rss_enclosure 中的函数)之外。

这是在原始提要中编写的,例如:

<enclosure url="http://www.samplevideourl.flv" length="18554755" type="video/x-flv" />

尝试添加其余的自定义字段并修改 rss2-feed.php 模板,以便它们显示在当前 RSS2 提要中每个段的末尾,现在将它们包含在内,例如:

...
<ratings_average>0</ratings_average>
<views>5</views>
</item>

但是,如果我更新联合帖子,或删除帖子并再次使用 feedwordpress 获取修改后的提要,这些都不会显示在联合帖子中。

有没有办法包含这些自定义字段,以便 feedwordpress 识别它们?

基本上需要联合与原始帖子相同的格式,包括所有自定义字段。

非常感谢

卡洛斯

I wonder if I could ask a Wordpress / RSS question I could't find an answer for around here,

Trying to syndicate posts via RSS in Wordpress using the FeedWordpress plugin as an RSS aggregator, each post in the original blog includes five custom fields that are important for its Theme functionality (the original and syndicating / receiving blog using the same theme).

The original RSS2 feed doesn't include these custom fields apart from one, being enclosure, that is defined in the default rss feed template (function in WP rss_enclosure).

This is written in the original feed such as:

<enclosure url="http://www.samplevideourl.flv" length="18554755" type="video/x-flv" />

Tried to add the rest of the custom fields modifying the rss2-feed.php template so they show at the end of each segment in the current RSS2 feed, now they are included as for example:

...
<ratings_average>0</ratings_average>
<views>5</views>
</item>

However, if I update the syndicated posts, or delete the posts and fetch the modified feed again with feedwordpress, none of these show in the syndicated posts.

Is there a way to include these custom fields so they are recognized by feedwordpress?

Basically need to syndicate the same format of the post as the original including all its custom fields.

Many Thanks

Carlos

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坐在坟头思考人生 2024-12-10 04:43:40

有一个线程涵盖了这一点:https://wordpress。 stackexchange.com/questions/3801/add-custom-fields-to-custom-post-type-rss

我已经浓缩了那里的答案以反映后来的改进(感谢 MikeSchinkel、prettyboymp 和使徒行传7)。

将其添加到主题的functions.php中:

/* IN ORDER TO VALIDATE you must add namespace   */
add_action('rss2_ns', 'my_rss2_ns');
function my_rss2_ns(){
    echo 'xmlns:mycustomfields="'.  get_bloginfo('wpurl').'"'."\n";
}

add_action('rss2_item', 'yoursite_rss2_item');
function yoursite_rss2_item() {
  if (get_post_type()=='my_custom_post_type') {
    $fields = array( 'field1', 'field2', 'field3' );
    $post_id = get_the_ID();
    foreach($fields as $field)
      if ($value = get_post_meta($post_id,$field,true))
        echo "<mycustomfields:{$field}>{$value}</mycustomfields:{$field}>\n";
  }
}

这会将所有自定义字段名称和值添加到网站的主要提要中。

请注意,对于具有多个值的自定义字段,需要进行修改,因为上述内容仅适用于单值字段,不适用于数组。

因此,

  1. 在您的主站点(您从中聚合的地方)添加上述功能。
  2. 在从属站点(您要聚合的地方)上,假设您安装了 FeedWordPress,请转到“SYNDICATION”->“Syndicate”。
  3. 单击 RSS 源的名称
  4. 转到自定义源设置并插入片段

There is a thread that covers this: https://wordpress.stackexchange.com/questions/3801/add-custom-fields-to-custom-post-type-rss

I've condensed the answers there to reflect the later improvements (thanks MikeSchinkel, prettyboymp and Acts7).

Add this to your theme's functions.php:

/* IN ORDER TO VALIDATE you must add namespace   */
add_action('rss2_ns', 'my_rss2_ns');
function my_rss2_ns(){
    echo 'xmlns:mycustomfields="'.  get_bloginfo('wpurl').'"'."\n";
}

add_action('rss2_item', 'yoursite_rss2_item');
function yoursite_rss2_item() {
  if (get_post_type()=='my_custom_post_type') {
    $fields = array( 'field1', 'field2', 'field3' );
    $post_id = get_the_ID();
    foreach($fields as $field)
      if ($value = get_post_meta($post_id,$field,true))
        echo "<mycustomfields:{$field}>{$value}</mycustomfields:{$field}>\n";
  }
}

This will add all custom field names and values to the site's main feed.

Note, for custom fields with more than one value, a modification is necessary as the above will only work for single value fields, not arrays.

So,

  1. On your Master site (where you are syndicating FROM) you add the above function.
  2. On the Slave site (where you are syndicating TO), assuming you have FeedWordPress installed, go to "SYNDICATION" ->
  3. Click on the name of the RSS feed
  4. Go to Custom Feed Settings and plug in the pieces
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文