Last active
June 30, 2016 10:12
-
-
Save morgyface/bd2b8fe2d40ce7dfd78a2a9c71856cf0 to your computer and use it in GitHub Desktop.
WordPress | Add additional image sizes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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] . '">'; | |
| ?> |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.