Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've seen the tutorials on how to grab the first image and display it in a post, and those on grabbing the post_thumbnail and using that in the RSS feed, but does anybody know how to grab the first image attached to a post and use that in the RSS feed? Thanks!

share|improve this question

3 Answers 3

i actually just finished working on a site that needed images in his feeds so i ended up using this:

function ba_post_image_feeds($content) {
    global $post,$posts;
    $first_img = '';
    ob_start();
    ob_end_clean();
    $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
    $first_img = $matches [1] [0];

    if(!empty($first_img)){   

        $content = '<div>' . $first_img . '</div>' . $content;
    }
    return $content;
}


add_filter('the_excerpt_rss', 'ba_post_image_feeds');
add_filter('the_content_feed', 'ba_post_image_feeds');
share|improve this answer
    
Thanks for the code. Not sure why it didn't work for me. I ended up using a plugin called RSS Custom Field Images and it works great! –  RodeoRamsey Mar 5 '11 at 23:26
up vote 0 down vote accepted

I ended up using a plugin called "RSS Custom Field Images" and it worked out of the box for me. I could even edit it to change the size of the image to something more manageable for me.

share|improve this answer

Here's another method.

function add_images_to_rss($var) {
    global $post;
    if(has_post_thumbnail($post->ID)) {
        $tid = get_post_thumbnail_id( $post->ID);
        $thumb = wp_get_attachment_image_src($tid, 'thumbnail');
        $thumb_meta = wp_get_attachment_metadata($tid);
        $up = wp_upload_dir();
        print '<enclosure type="'.get_post_mime_type($tid).'" length="'.filesize($up['basedir'].'/'.$thumb_meta['file']).'" url="'.$thumb[0].'" />';
    }
}
/* Technically this format is RSS2 only */
//    add_action('rss_item','add_images_to_rss');
    add_action('rss2_item','add_images_to_rss');
//    add_action('rdf_item','add_images_to_rss');
//    add_action('atom_entry','add_images_to_rss');
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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