这将满足Cha的需要:)
//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\\u the\\u post\\u缩略图,这也可以帮助您,因此您不需要创建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\\u image\\u size fn的几行匹配。所以如果你有add_image_size( \'small-square\', 100, 100, true );
你可以去拜访\'small-square\'
或者。
干杯