18
set_post_thumbnail_size( 80, 80, true ); // true for all post types

Suppose I have two post types in play on my blog: Post, Video and Product. I may want a 80x80 image size for my posts, but 100x100 for my videos and a 50x100 for my Products. To my knowledge, I am forced to create thumbnails for my Post images in the dimensions of 100x100 and also 50x100. And for my products, I'm forced to create thumbnails that are 80x80 and 100x100, even though I won't use those ever.

Is there a way to create image sizes that are based on post types so you don't end up making a bunch of images you don't plan on using, and wasting resources that could be used elsewhere?

  • I've run into exactly the same need. – MikeSchinkel Nov 26 '10 at 22:34
  • What happens if you use this function image_resize in the theme in the parts where you need that image size ?, you can also write a shorthand function for multiple uses depending on the context. – user17384 Jun 21 '12 at 20:07
  • Seems like an obvious oversight to me, has this really still not been addressed? – Nathan Hornby Feb 21 '17 at 17:18
12

There is no way to base the image sizes on the post type, as images are not yet attached to a post when you upload them and the intermediate image sizes are created (when you just add them via "New Media" they are not even attached to any post at all).

However, I recently created two plugins that together will do what you want. Virtual intermediate images will intercept the creation of the intermediate image sizes: they will not be created on disk, but still exist in the metadata array. This makes WordPress think they exist so you can use them like normal images. The companion plugin, On-Demand Resizer will create missing image sizes when they are requested. The first time the image is requested it is created, all subsequent requests will just be served the newly created image.

These plugins need a rewrite before I put them in the WordPress plugin directory, but they should be safe. Or at least have TODO comments where they are unsafe.

2

I posted some code on this other thread which sets a transient with the post type when uploading an image, and then checks for the value of that transient and adjusts the post-thumbnail size accordingly when the upload is finished. Its not perfect or bulletproof, but it should demonstrate the concept at least.

0

I came up with following solution. It does create images only for custom post type without creating any additional ones. The only problem is that if you use plugins like advanced custom fields, when trying to render the gallery field it only shows the default wordpress images sizes: 'thumbnail', 'medium', 'large' and you cannot access custom post image sizes created.

if ( function_exists ('add_image_size') ) {

function remove_default_img($sizes) {
    unset( $sizes['medium']);
    unset( $sizes['large']); 
    return $sizes;
}
add_filter('intermediate_image_sizes_advanced', 'remove_default_img');

$type = get_post_type($_REQUEST['post_id']);
// Post_Name Image Sizes ======================================================
if ( $type === "Custom_Post_Name" || $post_type === "Custom_Post_Name" ) {
add_image_size('custom_size_img', 165, 249, true);
}

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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