如何将贴文特色图片添加到RSS项目标签中?

时间:2012-07-24 作者:Michael Ecklund

I am able to add a post featured image to the RSS feed like so:

function insertThumbnailRSS($content) {
    global $post;
    if(has_post_thumbnail($post->ID)){
        $content = \'\'.get_the_post_thumbnail($post->ID, \'thumbnail\', array(\'alt\' => get_the_title(), \'title\' => get_the_title(), \'style\' => \'float:right;\')).\'\'.$content;
    }
    return $content;
}
add_filter(\'the_excerpt_rss\', \'insertThumbnailRSS\');
add_filter(\'the_content_feed\', \'insertThumbnailRSS\');
然而,在检查为RSS提要生成的XML时,我注意到它将特征图像粘贴到XML描述项标记中。

我怎样才能将帖子的特色图片插入到自己的RSS提要项目标签中,比如说“图片”,而不仅仅是将其与帖子的内容一起插入?

2 个回复
最合适的回答,由SO网友:codekipple 整理而成

您可以通过向挂钩“rss2\\u item”添加一个操作来完成此操作,如下所示:

add_action(\'rss2_item\', function(){
  global $post;

  $output = \'\';
  $thumbnail_ID = get_post_thumbnail_id( $post->ID );
  $thumbnail = wp_get_attachment_image_src($thumbnail_ID, \'thumbnail\');
  $output .= \'<post-thumbnail>\';
    $output .= \'<url>\'. $thumbnail[0] .\'</url>\';
    $output .= \'<width>\'. $thumbnail[1] .\'</width>\';
    $output .= \'<height>\'. $thumbnail[2] .\'</height>\';
    $output .= \'</post-thumbnail>\';

  echo $output;
});

SO网友:cfx

在codekipple和D\\N的基础上,我想在我的media:content 下面是我所做的:

function add_media_content_to_feed() {
  global $post;

  $post_id = $post->ID;

  if(!has_post_thumbnail($post)) {
    return;
  }

  $thumbnail_size = \'large\';
  $thumbnail_id   = get_post_thumbnail_id($post_id);

  $file           = image_get_intermediate_size(get_post_thumbnail_id(), $thumbnail_size);

  $url            = $file[\'url\'];
  $type           = $file[\'mime-type\'];
  $height         = $file[\'height\'];
  $width          = $file[\'width\'];
  $file_size      = \'\';

  $path           = $file[\'path\'];
  if($path && 0 !== strpos($path, \'/\') && !preg_match(\'|^.:\\\\\\|\', $path) && (($uploads = wp_get_upload_dir()) && false === $uploads[\'error\'])) {
    $path         = $uploads[\'basedir\']."/$path";
    $file_size    = filesize($path);
  }

  echo sprintf(__(\'<media:content url="%s" type="%s" medium="image" height="%s" width="%s" fileSize="%s" />\'),
    $url,
    $type,
    $height,
    $width,
    $file_size
  );

}
add_action(\'rss2_item\', \'add_media_content_to_feed\');
codekipple的回答实际上还将图像添加到了所有提要内容下面。我希望我的图像高于内容,所以我这样做了:

function add_featured_image_to_feed($content, $feed_type) {
  global $post;
  $post_id = $post->ID;
  if(has_post_thumbnail($post)) {
    $content = \'<div class="feed-image">\'.get_the_post_thumbnail($post_id, \'large\').\'</div>\'.$content;
  }
  return $content;
}
add_filter(\'the_content_feed\', \'add_featured_image_to_feed\', 10, 9999);

结束