Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save bappi-d-great/04e9d798976876a6b0c1 to your computer and use it in GitHub Desktop.
Save bappi-d-great/04e9d798976876a6b0c1 to your computer and use it in GitHub Desktop.
How to create different thumb size for different post type
<?php
add_action( 'pre-upload-ui', 'get_the_post_type' );
function get_the_post_type() {
$post_type = isset( $_REQUEST['post_type'] ) ? $_REQUEST['post_type'] : 'post';
set_transient( 'attached_post_type', $post_type );
}
add_filter( 'intermediate_image_sizes_advanced', 'add_image_size_for_post_type', 10 );
function add_image_size_for_post_type( $sizes ) {
$post_type = get_transient( 'attached_post_type' );
delete_transient( 'attached_post_type' );
/*
* BLOCK A
*
* This can be used if you want to set different size for only one post type. For multiple post type, check BLOCK B
*
*/
if( $post_type == 'page' ){
$sizes['post-thumbnail'] = array( 'width' => 275, 'height' => 175, 'crop' => true );
}
// END OF BLOCK A
/*
* BLOCK B
*
* This can be used if you want to set different sizes for different post types.
*
*/
$post_type_size = array(
'post' => array( 'width' => 275, 'height' => 175, 'crop' => true ),
'page' => array( 'width' => 375, 'height' => 475, 'crop' => true ),
'CPT' => array( 'width' => 475, 'height' => 675, 'crop' => true )
);
if( isset( $post_type_size[$post_type] ) ) $sizes['post-thumbnail'] = $post_type_size[$post_type];
// END OF BLOCK B
return $sizes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment