3

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!

2

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

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.

0

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

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.