This will do whatcha need :)
//Adding script to deligate Thumbnail Size
if ( function_exists( 'add_theme_support' ) ) {
add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size( 960, 276, true ); // default Post Thumbnail dimensions
}
//Set different Thumbnail Sizes for Later
if ( function_exists( 'add_image_size' ) ) {
add_image_size( 'large-thumb', 960, 276, true ); //(cropped)
add_image_size( 'medium-thumb', 605, 174, true ); //(cropped)
add_image_size( 'small-thumb', 288, 83, true ); //(cropped)
add_image_size( 'small-square', 100, 100, true ); //(cropped)
}
<?php if ( has_post_thumbnail() ) {
global $post; //I usually define this in the function that outputs this, fyi
echo '<a href="' . get_permalink( $post->ID ) . '" title="' . esc_attr( $post->post_title ) . '">';
echo get_the_post_thumbnail($thumbnail->ID, 'small-thumb', array( 'alt' => esc_attr( $post->post_title ), 'title' => esc_attr( $post->post_title ) ));
echo '</a>';
} else {
$thumbnails = get_posts(array('numberposts'=>1,'orderby'=>'rand','meta_key' => '_thumbnail_id'));
foreach ($thumbnails as $thumbnail) {
echo '<a href="' . get_permalink( $post->ID ) . '" title="' . esc_attr( $post->post_title ) . '">';
echo get_the_post_thumbnail($thumbnail->ID, 'small-thumb', array( 'alt' => esc_attr( $post->post_title ), 'title' => esc_attr( $post->post_title ) ));
echo '</a>';
}
}
?>
It's using get_the_post_thumbnail which may also help you so you didn't need to create a bunch of fn code that WordPress can already handle for you, just a thought.
This uses $thumbnails = get_posts(array('numberposts'=>1,'orderby'=>'rand','meta_key' => '_thumbnail_id'));
to grab a random one if one is not present, this may help you moving forward.
This bit echo get_the_post_thumbnail($thumbnail->ID, 'small-thumb', array( 'alt' => esc_attr( $post->post_title ), 'title' => esc_attr( $post->post_title ) ));
notice the 'small-thumb'
it gets matched to those add_image_size fn's we put together up a few lines. So if you had add_image_size( 'small-square', 100, 100, true );
you could call on 'small-square'
alternatively.
Cheers