Archived topic
Custom Image Sizes for Feature Image Not Staying Active
2 replies · Started by alex2k5 on January 4, 2021
Hello,
I am building out a new theme. Using a child theme. Very early. Want to use custom image sizes in various locations.
Added this to the functions.php file of the child theme:
function tu_add_image_sizes() {
add_image_size( 'HOME_THUMB', 320, 180, true );
add_image_size( 'FEATURED_HOME', 400, 225, true );
add_image_size( 'SINGLE_SPLASH', 800, 450, true );
add_image_size( 'FEATURED_MICRO', 128, 72, true );
}
add_action( 'after_setup_theme', 'tu_add_image_sizes' );
These now show up in default Wordpress customizer under layout -> blog -> media attachment size
If I choose one of them and save - refreshing a test post doesn't change the image used. Still showing full size image.
If I go back into the customizer - layout -> blog -> media attachment size - the choice I made earlier is not active, instead it's listed as "full" again, like I never changed it.
I have re-uploaded and changed the default image after the functions.php change, and manually checked to see if the images exist (adding -800x450 to end of image url), and they do. But GP will not call them into the page.
Attempting to call SINGLE_SPLASH into a single post.
Choosing wordpress default size thumbnail for archives does "stick" and works properly.
Played around a bit more with this, got it working. Not 100% sure why.
Switched to this method of adding featured images, from help docs, which is slightly different than what I found on the forums:
add_action( 'init', function() {
add_image_size( 'home-thumb', 320, 180, true );
add_image_size( 'featured-home', 400, 225, true );
add_image_size( 'single-splash', 800, 450, true );
add_image_size( 'featured-micro', 128, 72, true );
} );
I also switched the size names to lowercase with dashes rather than uppercase with underscores.
Once I did that and re-uploaded/cropped the image - they were called and displayed properly.
So, working right now I think.
Glad you got it sorted - thanks for sharing the working code :)