开发者

Syndicating custom fields in Wordpress via RSS

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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜