要实现自定义大小(更新/保存帖子后),请尝试以下操作。。。
// Step 1 - define our custom image size so we can call it instead of the system defined default \'post-thumbnail\'
add_image_size( \'slider_image\', 800, 300, true );
// Step 2 - replace the meta box with ours for our CPT \'slider-image\' only, especially so we can set our own callback. The rename is nice, too.
add_action(\'do_meta_boxes\', \'wpse33063_move_meta_box\');
function wpse33063_move_meta_box(){
remove_meta_box( \'postimagediv\', \'slider-image\', \'side\' );
add_meta_box(\'postimagediv\', __(\'Add Your Slider Image\'), \'wpse33063_post_thumbnail_meta_box\', \'slider-image\', \'normal\', \'high\');
}
// Step 3 - custom callback to call our functional replacement for _wp_post_thumbnail_html for our CPT \'slider-image\' only
function wpse33063_post_thumbnail_meta_box( $post ) {
$thumbnail_id = get_post_meta( $post->ID, \'_thumbnail_id\', true );
echo wpse33063_wp_post_thumbnail_html( $thumbnail_id, $post->ID );
}
// Step 4 - replace _wp_post_thumbnail_html with our version that calls our thumbnail \'slider_image\' instead of the default \'post-thumbnail\'
// - we could do more here, like adjust the content width variable, but not entirely necessary. The custom image size we defined will
// - handle most of it, plus the admin section these days has responsive css, so the image doesn\'t blow-out column width in any event.
function wpse33063_wp_post_thumbnail_html( $thumbnail_id = null, $post = null ) {
global $content_width, $_wp_additional_image_sizes;
$post = get_post( $post );
$post_type_object = get_post_type_object( $post->post_type );
$set_thumbnail_link = \'<p class="hide-if-no-js"><a title="%s" href="%s" id="set-post-thumbnail" class="thickbox">%s</a></p>\';
$upload_iframe_src = get_upload_iframe_src( \'image\', $post->ID );
$content = sprintf( $set_thumbnail_link,
esc_attr( $post_type_object->labels->set_featured_image ),
esc_url( $upload_iframe_src ),
esc_html( $post_type_object->labels->set_featured_image )
);
if ( $thumbnail_id && get_post( $thumbnail_id ) ) {
$old_content_width = $content_width;
$content_width = 266;
if ( !isset( $_wp_additional_image_sizes[\'slider_image\'] ) ) // use our custom image size instead of \'post-thumbnail\'
$thumbnail_html = wp_get_attachment_image( $thumbnail_id, array( $content_width, $content_width ) );
else
$thumbnail_html = wp_get_attachment_image( $thumbnail_id, \'slider_image\' ); // use our custom image size instead of \'post-thumbnail\'
if ( !empty( $thumbnail_html ) ) {
$ajax_nonce = wp_create_nonce( \'set_post_thumbnail-\' . $post->ID );
$content = sprintf( $set_thumbnail_link,
esc_attr( $post_type_object->labels->set_featured_image ),
esc_url( $upload_iframe_src ),
$thumbnail_html
);
$content .= \'<p class="hide-if-no-js"><a href="#" id="remove-post-thumbnail" onclick="WPRemoveThumbnail(\\\'\' . $ajax_nonce . \'\\\');return false;">\' . esc_html( $post_type_object->labels->remove_featured_image ) . \'</a></p>\';
}
$content_width = $old_content_width;
}
/**
* Filter the admin post thumbnail HTML markup to return.
*
* @since 2.9.0
*
* @param string $content Admin post thumbnail HTML markup.
* @param int $post_id Post ID.
*/
return apply_filters( \'admin_post_thumbnail_html\', $content, $post->ID );
}