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 badges2 Answers
Reset to default 1No, 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
版权声明:本文标题:categories - Add Image Size for one Media Category Only 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741480310a2381129.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论