Images in Drupal RSS Feed, without additional modules

There is an easier way to include the image associated with your node in a Drupal Views RSS feed. No additional modules are required.

Firstly you need to add the Media RSS namespace to your views-view-rss.tpl.php:

<rss version="2.0" xml:base="<?php print $link; ?>"<?php print $namespaces; ?> xmlns:media="http://search.yahoo.com/mrss/">

Secondly, in the row style template for your RSS feed (mine is called views-view-row-rss–market-news-rss-feed–feed.tpl.php), you need add a media element, and enter your image field name, your image style, and your dimensions:

<?php

/**
 * @file
 * Default view template to display a item in an RSS feed.
 *
 * @ingroup views_templates
 */
?>
  <item>
    <title><?php print $title; ?></title>
    <link><?php print $link; ?></link>
    <description><?php print $description; ?></description>
    <media:content url="<?php print image_style_url('news_old_style__300x250_', $node->field_image['und'][0]['uri']); ?>" type="image/jpeg" medium="image" height="250" width="300" lang="en" />
    <?php print $item_elements; ?>
  </item>

Lastly, make the field data available to the template above by adding the following to your template.php

function nadex_preprocess_views_view_row_rss(&$vars) {
  $view     = &$vars['view'];
  $options  = &$vars['options'];
  $item     = &$vars['row'];
 
  // Use the [id] of the returned results to determine the nid in [results]
  $result = &$vars['view']->result;
  $id   = &$vars['id'];
  $node   = node_load( $result[$id-1]->nid );
  $vars['node'] = $node;
}

Thanks to these guys for the inspiration:

Leave a comment

Your email address will not be published. Required fields are marked *