Created
September 20, 2012 10:17
-
-
Save jartes/3755078 to your computer and use it in GitHub Desktop.
Get First Image from a WordPress Post
This file contains 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
get_thumbnail_image_src() { | |
/** | |
* @todo | |
* * Return or echo | |
* | |
* | |
*/ | |
global $post; | |
if ( has_post_thumbnail( $post->ID ) ) { | |
$src = wp_get_attachment_image_src( get_post_thumbnail_id() ); | |
return $src; | |
} | |
$first_img = ''; | |
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches); | |
$first_img = $matches [1] [0]; | |
if(empty($first_img)){ //Defines a default image | |
$first_img = get_bloginfo('template_directory') . "/images/default.jpg"; | |
} | |
return $first_img; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This function retrieves the source of the first image of a WordPress post. But if the post have a post thumbnail, it will return the source of it.