Looking at the heading of the question it may seem like a duplicate question, but it is not. Let me explain a few things before anyone downvote or make it a duplicate.
As far as I know in WordPress the only way to make custom image size is by using the add_image_size()
. Now let's say I have a CPT called Testimonials
and at the time of registering the CPT I have added thumbnail
support.
Then in my functions.php
if I have like the following:
<?php
add_action( 'after_setup_theme', function() {
// Custom Image Sizes
add_image_size( "my-testimonial", 100, 100, true);
});
Then in my template while loading the thumbnail of the CPT I can do this:
the_post_thumbnail('my-testimonial');
And this will give me a 100x100 image as the thumbnail.
Problem With The Above Approach
Though the above approach works fine, it has a major downside. As add_image_size()
is global, after this point any image I upload, whether that be for a blog post, a page, this CPT or any other CPT, each of those image upload will have a 100x100 version in my server which is simply wasting both server execution & storage resource.
Moreover, if I use any image optimization API, they are going to optimize all the images regardless of whether I use them or not and charge me accordingly.
What I am Looking For
So, what I am looking for is a way to specify the thumbnail size of specific CPT so that if I upload any featured image for that CPT then only those images will be sized to what I want but it will not crop any other image outside of that scope.
I have Googled a lot but couldn't find any solution to this dilemma. Moreover, most of the answers are old and now WP 5.4 has been released, so I was wondering if there is any way to accomplish this now.