set_post_thumbnail_size()
Registers an image size for the post thumbnail.
No Hooks.
Return
null
. Nothing (null).
Usage
set_post_thumbnail_size( $width, $height, $crop );
- $width(int)
- Image width in pixels.
- $height(int)
- Image height in pixels.
- $crop(true|false|array)
Image cropping behavior. If false, the image will be scaled (default). If true, image will be cropped to the specified dimensions using center positions. If an array, the image will be cropped using the array to specify the crop location:
Default: false
-
0(string)
The x crop position. Accepts 'left' 'center', or 'right'. - 1(string)
The y crop position. Accepts 'top', 'center', or 'bottom'.
-
Examples
#1 Basic example
Define the use of thumbnails in the template and specify the size of the post thumbnail:
if ( function_exists( 'add_theme_support' ) ) { add_theme_support( 'post-thumbnails' ); set_post_thumbnail_size( 150, 150 ); }
#2 Cropping
Resize proportionally: Set the default thumbnail size to 50x50 pixels. The image will be resized by the smallest side. On the larger side, the edges will be cropped.
set_post_thumbnail_size( 50, 50 ); // shrink the picture
Cropping: set the default thumbnail size to 50x50 pixels. The image will be cropped: the central part of the image will be taken with a size of 50x50 pixels:
set_post_thumbnail_size( 50, 50, true ); // cropping
Cropping: Set the default Post Thumbnail size by cropping the image from top left:
// 50 pixels wide by 50 pixels tall, crop from the top left corner set_post_thumbnail_size( 50, 50, array( 'top', 'left') );
Cropping: Set the default Post Thumbnail size by cropping the image from the center:
// 50 pixels wide by 50 pixels tall, crop from the center set_post_thumbnail_size( 50, 50, array( 'center', 'center') );
See add_image_size() for more information about cropping.
Note: This function will not resize your existing images. To regenerate existing images in the new sizes, use the Regenerate Thumbnails plugin.
Notes
- See: add_image_size() for details on cropping behavior.
Changelog
Since 2.9.0 | Introduced. |
set_post_thumbnail_size() set post thumbnail size code WP 6.7.2
function set_post_thumbnail_size( $width = 0, $height = 0, $crop = false ) { add_image_size( 'post-thumbnail', $width, $height, $crop ); }