I’m particular about a lot of things, including my excerpts and featured images. The biggest problem I have is remembering what my featured image size should be. You see, I do slap up a custom image, just for that, most of the time, and remembering that the size should be 900×220 every day is something I just can’t do. So I lamented, as I checked for the 99th time, that I should be able to pull in the size!
What I want is this:
And yes, I did it with this in my functions.php for my theme:
//* Add new image sizes add_image_size( 'featured-image', 900, 220, true ); //* Change Featured Image to remind me what the size is add_filter( 'admin_post_thumbnail_html', 'helf_admin_post_thumbnail_html' ); function helf_admin_post_thumbnail_html( $content ) { // Get featured image size global $_wp_additional_image_sizes; $featured_image = $_wp_additional_image_sizes['featured-image']['width'].'x'.$_wp_additional_image_sizes['featured-image']['height']; // Apply $imagesize = '<p>Image Size:' . $featured_image . 'px</p>'; $content = $imagesize . $content; return $content; }
The trick there is I have to know the name of my featured image. Since I use Genesis, it’s usually named ‘featured-image’ but it could be anything. That also means on my Theme Settings I have this set:
And that has to match. So, since I am running Genesis, it’s a quick change to put in this check instead:
add_filter( 'admin_post_thumbnail_html', 'helf_admin_post_thumbnail_html' ); function helf_admin_post_thumbnail_html( $content ) { // Define what the name of our featured image size is $genesis = get_option('genesis-settings'); $genesis_image_size = $genesis['image_size']; // Get featured image size global $_wp_additional_image_sizes; $my_featured_image = $_wp_additional_image_sizes[$genesis_image_size]['width'].'x'.$_wp_additional_image_sizes[$genesis_image_size]['height']; // Apply $imagesize = '<p>Image Size: ' . $my_featured_image . 'px</p>'; $content = $imagesize . $content; return $content; }
The only thing I couldn’t get to work was an if-check around the filter so I could make sure I really am running Genesis. Future improvements, I suppose.
Comments
3 responses to “Featured Image Size”
Dang, that’s a handy little piece of code you’ve got there. Thanks for sharing. I usually just rely on the automatic resizer handling it for me, but knowing the size before hand would make a lot more sense.
@Ryan Hellyer: The problem with the resizer is that it’s not really a hard crop. So as much as I love it, I get images that are correct on one side, but not always both, and then my layout gets skewed. Like once site has a featured image of 900×220 (That screenshot is the real site) so that’s long and thin. I don’t always have a 900px wide photo, so I get weird short ones π
By always knowing, I always remember to manually make my featured image and not allow automagic to make it acceptable, I always make it right <3
Agree with Ryan, I’m saving this little gem in my Evernote GenesisWP notebook.