Created
May 9, 2014 11:00
-
-
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()). '"/>';
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is there easier ways of doing this?