michaelwilhelmsen
5/9/2014 - 11:00 AM

Using the built in Simplepie library in Wordpress, this function fetches the first img of each post. Display it using: echo '<img src="' .g

Using the built in Simplepie library in Wordpress, this function fetches the first img of each post.

Display it using: echo '';

/**
 * Gets RSS Image Item Link
 *
 * Place the following inside rss markup
 * to display the the first image of that post:
 * echo '<img src="' .get_first_image_url($item->get_content()). '"/>';
 *
 */
function get_first_image_url($html) {
	if (preg_match('/<img.+?src="(.+?)"/', $html, $matches)) {
		return $matches[1];
	}
	
	else return 'url_of_default_image.jpg';
}