2

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 2

3

admin_post_thumbnail_size takes three parameters:

  1. $thumb_size: selected Thumb Size if you do nothing in the filter.

  2. $thumbnail_id: Thumbnail attachment ID.

  3. $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);
0
1

There is no filter to do this for just a certain post_type but you can use get_post_type( esc_attr( $_GET['post'] ) ) to save 1 line of code basically.

You can also take out the else statement since your if statement has a return it won't reach after the if anyway. Other than that this is basically what you do.

function custom_admin_thumb_size( $thumb_size ){

    if( 'slider' == get_post_type( esc_attr( $_GET['post'] ) ) ) {

        return array( 1000, 400 );

    }

    return array( 266, 266);

}
add_filter( 'admin_post_thumbnail_size', 'custom_admin_thumb_size' );
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.