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!
3 Answers
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');