set_post_thumbnail_size()
Sets the default size of the post thumbnail.
Registers or overrides the parameters of the thumbnail size post-thumbnail, which in WP is used as the post thumbnail.
After setting this function, the thumbnail can be obtained via get_the_post_thumbnail():
echo get_the_post_thumbnail( $page->ID );
This is a wrapper for the function add_image_size() with the preset size name for the thumbnail 'post-thumbnail'. The function exists so that the thumbnail size name does not change - it must remain exactly that.
To be able to define a post thumbnail image, you need to enable this feature with the function: add_theme_support( 'post-thumbnails' ) in the theme file funсtions.php.
To register a new thumbnail size, use add_image_size().
No Hooks.
Returns
null. The function does not return anything.
Usage
set_post_thumbnail_size( $width, $height, $crop );
- $width(int)
- Width of the thumbnail (in pixels).
- $height(int)
- Height of the thumbnail (in pixels).
- $crop(boolean)
- Crop the image (true - a piece of the image will be taken according to the specified dimensions) or simply resize (false - the image will be resized proportionally, the excess will be cut off).
Default: false
Examples
All examples are intended for use in the theme file functions.php.
#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.9
function set_post_thumbnail_size( $width = 0, $height = 0, $crop = false ) {
add_image_size( 'post-thumbnail', $width, $height, $crop );
}