Forked from bappi-d-great/How to create different thumb size for different post type.php
Last active
August 29, 2015 14:12
-
-
Save ThemeMetric/0172a7863b560eb8d645 to your computer and use it in GitHub Desktop.
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