最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - How to Add Image to Wordpress RSS-Feed with no Plug-in?

programmeradmin5浏览0评论

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 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.

Share Improve this question edited Aug 28, 2014 at 9:31 zkanoca asked Aug 28, 2014 at 9:18 zkanocazkanoca 2031 gold badge2 silver badges8 bronze badges 1
  • I think you are adding featured image in content, not on feeds. Check the answer I posted. – Robert hue Commented Aug 28, 2014 at 9:27
Add a comment  | 

5 Answers 5

Reset to default 12

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' );

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/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.

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.

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;
 }
});

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/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>
发布评论

评论列表(0)

  1. 暂无评论