Skip to content

Instantly share code, notes, and snippets.

@morgyface
Last active June 30, 2016 10:12
Show Gist options
  • Select an option

  • Save morgyface/bd2b8fe2d40ce7dfd78a2a9c71856cf0 to your computer and use it in GitHub Desktop.

Select an option

Save morgyface/bd2b8fe2d40ce7dfd78a2a9c71856cf0 to your computer and use it in GitHub Desktop.
WordPress | Add additional image sizes
<?php
add_action( 'after_setup_theme', 'add_image_sizes' );
function add_image_sizes() {
add_image_size( 'tiny', 80, 60, true ); // 80 pixels wide by 60 pixels tall, hard crop mode
add_image_size( 'super', 1600, 1200, true );
}
// Use them like this
if ( has_post_thumbnail() ) {
the_post_thumbnail( 'super' );
}
// Or
$postid = $post->ID;
$imageid = get_post_thumbnail_id($postid);
$imageurl = wp_get_attachment_image_src($imageid, 'tiny');
echo '<img src="' . $imageurl[0] . '">';
?>
@morgyface
Copy link
Author

Going beyond Thumbnail, Medium & Large

Sometimes we need more than the three image size options as specified in Settings > Media. Here you'll find the action you need to add to your themes functions.php file to create additional sizes. I've also added some code below to show you how to add them in to your themes template files like single.php and page.php.

Also check out the official code reference here for more information on cropping and sizing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment