I want to change the size of the featued image for one custom post type only. This is what I currently have:
function custom_admin_thumb_size($thumb_size){
global $post_type;
if( 'slider' == $post_type ){
return array(1000,400);
}else{
return array(266,266);
}
}
add_filter( 'admin_post_thumbnail_size', 'custom_admin_thumb_size');
This function does what I expected but I was wondering if there is any better method to call the custom post type “slider” without touching the others.
Thanks in advance
2 Answers
admin_post_thumbnail_size
takes three parameters:
-
$thumb_size: selected Thumb Size if you do nothing in the filter.
-
$thumbnail_id: Thumbnail attachment ID.
-
$post: associated WP_Post instance
So you may make use of these parameters to have a better control in your CODE. Use the CODE like below:
function custom_admin_thumb_size( $thumb_size, $thumbnail_id, $post ) {
if( 'slider' === $post->post_type ) {
return array( 1000, 400 );
}
return $thumb_size;
}
add_filter( 'admin_post_thumbnail_size', 'custom_admin_thumb_size', 10, 3);