admin管理员组

文章数量:1289509

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.

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.

Share Improve this question asked Feb 18, 2015 at 23:07 BryanBryan 2911 gold badge3 silver badges7 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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.

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

https://gist.github/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.

本文标签: categoriesAdd Image Size for one Media Category Only