9

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
11

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' );
| improve this answer | |
  • 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
6

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.

| improve this answer | |
  • 4
    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
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.

| improve this answer | |
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;
 }
});
| improve this answer | |
0

Here is how I fetch the images along with the link and post title, from external rss feed.

<div class="row">

    <?php
    /*
     * Get latest blog entries from RSS feed blog
     * Sort the entries by published date
     * Get Featured Image SRC and ALT attributes using Regexp
     * Loop and repeat it 4 times, to display 4 articles
     */
    $feed = 'https://www.somewpsite.com/feed/';
    $entries = array();
    $xml = simplexml_load_file($feed);
    $entries = array_merge($entries, $xml->xpath("//item"));
    //Sort feed entries by pubDate
    usort($entries, function ($feed1, $feed2) {
        return strtotime($feed2->pubDate) - strtotime($feed1->pubDate);
    });
    ?>

    <?php
    $counter = 0;
    foreach ($entries as $entry) {
        //get Featured Image
        //Enter entry blog Content
        $blogContent = $entry->description;
        //regexp to find img attribute
        preg_match('/(<img[^>]+>)/i', $blogContent, $matches);
        //store the first img to var
        $featuredImage = $matches[0];
        //Get src attr with regexp
        preg_match('@src="([^"]+)"@', $featuredImage, $getSrc);
        //Get alt attr with regexp
        preg_match('@alt="([^"]+)"@', $featuredImage, $getAlt);
        //Store the filtered Attributes to display them.
        $imgAlt = array_pop($getAlt);
        $imgSrc = array_pop($getSrc);
        // remove url parameters
        $url = $entry->link;
        $url = strtok($url, '?');
        //Set counter to iterate over 4 items and then stop the loop.
        if ($counter <= 3) { ?>

            <div class="col-12 col-sm-12 col-md-6 col-lg-3 col-xl-3">
                <div class="imgContainer">
                    <a href="<?php echo $url ?>">
                        <img src="<?php echo $imgSrc ?>" alt="<?php echo $imgAlt; ?>" class="img-fluid">
                    </a>
                </div>
                <h4>
                    <a class="blog-entry-link" href="<?php echo $url ?>"><?= $entry->title ?></a>
                </h4>
            </div>

            <?php
            $counter++;
        } // end counter condition
    }// end for loop ?>
</div>
| improve this answer | |

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