Created
July 17, 2013 02:36
-
-
Save tcrsavage/6017245 to your computer and use it in GitHub Desktop.
Default the_post_thumbnail() and get_the_post_thumbnail() to return the first image in post content if no featured image is set
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
//If no post thumbnail exists, get the first image in the post and return that instead | |
add_filter( 'post_thumbnail_html', function( $html, $post_id, $post_thumbnail_id, $size, $attr ) { | |
if ( $html ) | |
return $html; | |
$found = preg_match( '/<img.+?class=\".+?([0-9]+).*\/>/', get_post( $post_id )->post_content, $matches ); | |
if ( $found ) | |
$html = wp_get_attachment_image( end( $matches ), $size, false, $attr ); | |
return $html; | |
}, 10, 5 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment