这将满足需要:)
//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>';
}
}
?>
它正在使用get_the_post_thumbnail,这也可能对您有所帮助,因此您无需创建一堆WordPress已经可以为您处理的fn代码。
这款采用$thumbnails = get_posts(array('numberposts'=>1,'orderby'=>'rand','meta_key' => '_thumbnail_id'));
抢随机的,如果一个不存在,这可能会帮助你前进。
这一点echo get_the_post_thumbnail($thumbnail->ID, 'small-thumb', array( 'alt' => esc_attr( $post->post_title ), 'title' => esc_attr( $post->post_title ) ));
注意'small-thumb'
它与我们将几行放在一起的add_image_size fn匹配。因此,如果您有add_image_size( 'small-square', 100, 100, true );
,可以选择致电'small-square'
。
干杯