joshua.paling
joshua.paling

Reputation: 13952

Different thumbnails sizes for different custom post types, and creating thumbnails for featured images only

I've got a wordpress site with a few custom post types. Each custom post type shows a list page, basically displaying a featured image and a title, and then you can click through to a detail page with a bunch more images.

I only need to resize uploaded "feature images". If an uploaded image isn't used as a feature image, I don't need a thumbnail for it. Furthermore, each custom post type shows a different sized feature image.

So, what I want to do is say:

a) Only create thumbnails for feature images

b) Create certain sizes for certain post types, and not for others.

Is this possible? At the moment, EVERY uploaded image is getting 5 thumbnail sizes, and my wp-content directory is WAAAY bigger than it needs to be!

Thanks in advance.

Upvotes: 3

Views: 1979

Answers (2)

scrubmx
scrubmx

Reputation: 2556

Ok I found a solution using the intermediate_image_sizes filter hook. Place this code in your functions.php and replace the 'post_type' and 'img_size_name' with the names of your post and desired image size.

add_filter( 'intermediate_image_sizes', function($sizes){
    $type = get_post_type($_REQUEST['post_id']);
    foreach($sizes as $key => $value){
        if($type=='post_type'  &&  $value != 'img_size_name'){
            unset($sizes[$key]);
        }
    }
    return $sizes;
});

Upvotes: 10

RCWD
RCWD

Reputation: 29

To avoid waste of space, the best solution is to use phpthumb (http://phpthumb.sourceforge.net/) inside your page templates.

Upvotes: 2

Related Questions