1

add_image_size is great, but if I want to create a swatch size for colors/patterns on my site, I don't need the swatch size for all of my other images.

Is there any way to apply image size by media category?

I am using the Media Category plugin and this would be the perfect solution for generating image sizes based upon type.

2 Answers 2

1

No, images are global and you might change which content they are associated with at which point you will need sizes that do not exist.

You can generate images "on the fly" but this may slow down page generation (depending on the way generated image information is cached) and might break code which "pushes" into CDN or cloud storage like S3.

WordPress themes and plugins seem to generate way too many images, but since most of the images generated that way are relatively small, and storage is cheap, it doesn't seem to be sopmething that you should worry about enough to write code to optimize it.

0

I've used the code linked to below for just such a requirement.

https://gist.github.com/seedprod/1367237

Doing this, you don't create a new image size every time you upload a file via the Media Editor, but only when necessary. So essentially, you just go in and add the call to resize the image "on the fly" in your templates where needed.

So you'd add that code to your functions.php, then do something like this in your category.php file:

<?php if (is_category(2)) {
    $thumb = get_post_thumbnail_id(); 
    $image = vt_resize( $thumb, '', 140, 110, true );
?>
<img src="<?php echo $image[url]; ?>" width="<?php echo $image[width]; ?>" height="<?php echo $image[height]; ?>" />

Every post in the category with the ID 2 would then be displayed on that page with the 140 x 110 cropped image.

Could be used easily in a single.php file, too, with something like:

<?php if (in_category(2)) {
    $thumb = get_post_thumbnail_id(); 
    $image = vt_resize( $thumb, '', 140, 110, true );
?>
<img src="<?php echo $image[url]; ?>" width="<?php echo $image[width]; ?>" height="<?php echo $image[height]; ?>" />

You just set it up in your template once, and you never worry about it again. It's the closest thing to not generating additional thumbnails for every image you'll come across, and even if you could write some code to limit the generation of certain image sizes to only happen on posts in certain categories, you'd still need to add the code that does this to functions.php or use a plugin, and then you'd also still need to edit your category.php or single.php or whatever to differentiate between categories.

2
  • 1
    Thanks Nathan. Thats interesting... But I'd really like the thumbnails auto generated. I just want certain image sizes for specific media categories.
    – Bryan
    Feb 24, 2015 at 21:16
  • See my edited answer, the thumbnails would be auto-generated.
    – Nathan
    Feb 25, 2015 at 1:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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