Skip to content

Instantly share code, notes, and snippets.

@tcrsavage
Created July 17, 2013 02:36

Revisions

  1. tcrsavage created this gist Jul 17, 2013.
    14 changes: 14 additions & 0 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    //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 );