Skip to content

Instantly share code, notes, and snippets.

@michaelwilhelmsen
Created May 9, 2014 11:00
Show Gist options
  • Save michaelwilhelmsen/299da2e4f96d5e3ffb37 to your computer and use it in GitHub Desktop.
Save michaelwilhelmsen/299da2e4f96d5e3ffb37 to your computer and use it in GitHub Desktop.
Using the built in Simplepie library in Wordpress, this function fetches the first img of each post. Display it using: echo '<img src="' .get_first_image_url($item->get_content()). '"/>';
/**
* 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';
}
@michaelwilhelmsen
Copy link
Author

Is there easier ways of doing this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment