5

I have searched for adding featuring image to rss feed with no plug-in for WordPress. I have found some examples and applied how it was instructed. But All I have got now is nothing. Still can't reach the image.

The last and the best (according to me) solution (as told here) I have found is adding the following code to current theme's functions.php

function featured_image_in_feed( $content ) {
    global $post;
    if( is_feed() ) {
        if ( has_post_thumbnail( $post->ID ) ){
            $output = get_the_post_thumbnail( $post->ID, 'medium', array( 'style' => 'float:right; margin:0 0 10px 10px;' ) );
            $content = $output . $content;
        }
    }
    return $content;
}
add_filter( 'the_content', 'featured_image_in_feed' );

I have not seen any change. Is there something else to be done?

What I want to do is adding featuring image as another key in the XML. It would be easier to show it for my slider.

  • I think you are adding featured image in content, not on feeds. Check the answer I posted. – Robert hue Aug 28 '14 at 9:27
8

Here is a great example. How to display featured post thumbnails in WordPress feeds

paste this code snippet in your theme functions.php file

// display featured post thumbnails in WordPress feeds
function wcs_post_thumbnails_in_feeds( $content ) {
    global $post;
    if( has_post_thumbnail( $post->ID ) ) {
        $content = '<p>' . get_the_post_thumbnail( $post->ID ) . '</p>' . $content;
    }
    return $content;
}
add_filter( 'the_excerpt_rss', 'wcs_post_thumbnails_in_feeds' );
add_filter( 'the_content_feed', 'wcs_post_thumbnails_in_feeds' );
  • How can I add imgURL and imgTitle as seperate keys in XML? – zkanoca Aug 28 '14 at 9:28
  • For that you will have to modify Feed template. Here is a tutorial for that. digwp.com/2012/10/customizing-wordpress-feeds/… – Robert hue Aug 28 '14 at 9:33
  • I'd better manipulate description string. (: – zkanoca Aug 28 '14 at 9:39
  • What exactly do you want to do? If you can explain more, may be someone here can help you. – Robert hue Aug 28 '14 at 9:47
  • I want to show featured news's images and titles on a Bootstrap's carousel component. Trying to write a .php. And your help is good enough to solve my problem. Thanks. I will extract image path from description key. – zkanoca Aug 28 '14 at 9:52
2

I tried the selected answer and got a really big image in my feed. I would recommend adding an image size to the code.

// display featured post thumbnails in RSS feeds
function WPGood_rss_thumbs( $content ) {
    global $post;
    if( has_post_thumbnail( $post->ID ) ) {
        $content = '<figure>' . get_the_post_thumbnail( $post->ID, 'thumbnail' ) . '</figure>' . $content;
    }
    return $content;
}
add_filter( 'the_excerpt_rss', 'WPGood_rss_thumbs' );
add_filter( 'the_content_feed', 'WPGood_rss_thumbs' );

I used 'thumbnail' for my feed, but 'medium' might work better for some sites.

2

Based on the notes here and many other resources I read, I came up with this solution specifically to work with Mailchimp RSS to Email converter with the feed from Wordpress. Their templates use the <media:content> extension to the item element to populate their image macro. This code goes in the functions.php of the theme.

// Add namespace for media:image element used below
add_filter( 'rss2_ns', function(){
  echo 'xmlns:media="http://search.yahoo.com/mrss/"';
});

// insert the image object into the RSS item (see MB-191)
add_action('rss2_item', function(){
  global $post;
  if (has_post_thumbnail($post->ID)){
    $thumbnail_ID = get_post_thumbnail_id($post->ID);
    $thumbnail = wp_get_attachment_image_src($thumbnail_ID, 'medium');
    if (is_array($thumbnail)) {
      echo '<media:content medium="image" url="' . $thumbnail[0]
        . '" width="' . $thumbnail[1] . '" height="' . $thumbnail[2] . '" />';
    }
  }
});

The choice of image size 'medium' can also be 'thumbnail' if you want one smaller.

  • 1
    Thanks. Just what I needed to make other aggregators to actually find an image. Just adding an image tag to the content is not enough – Juergen Jun 7 '18 at 9:38
0

I tried these answers above and was not able to get it to work. It kept adding my image in the description area.

I found this on another site and modified it a bit and voila it started working.

add_action('rss2_item', function(){
global $post;
 if(has_post_thumbnail($post->ID)){
  $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;
 }
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.