Forked from bappi-d-great/How to create different thumb size for different post type.php
Created
November 16, 2017 10:32
-
-
Save mayeenulislam/d24083c41c1c33882135ce28dc3d639c to your computer and use it in GitHub Desktop.
How to create different thumb size for different post type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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