How to set post thumbnail size in wordpress

What is the difference between set_post_thumbnail_kích cỡ và add_image_size? Is set_post_thumbnail_size for all images, & add_image_kích thước for sizing images individually?

Thanks.

Bạn đang xem: How to set post thumbnail size in wordpress


*

When an image is uploaded in nguyenminhchau.com, it is saved in its original kích thước, & also as some resized copies in different sizes. In this way it becomes easy to use different sizes, of the same image, for different purposes.

By mặc định, nguyenminhchau.com creates 3 copies in 3 different sizes:

"thumb""medium""large"

The kích cỡ in pixels for these 3 sizes can be mix in the nguyenminhchau.com dashboard, under the thực đơn Settings -> Media.

Now, the function add_image_kích thước is used khổng lồ register a new kích cỡ, that adds lớn the 3 defaults.

Every size registered can be retrieved using its name, in some nguyenminhchau.com function, e.g. wp_get_attachment_image_src, wp_get_attachment_image, wp_get_attachment_liên kết.

All these functions accept a parameter $kích thước that should be the name of one of the registered sizes (one of the 3 standards, or one of the custom sizes registered with add_image_size).

If "full" is used as $size argument, the original image is returned, the one not resized.

In addition lớn the functions mentioned above sầu, there are two other functions that make use of image sizes: get_the_post_thumbnail & the_post_thumbnail.

Xem thêm: Đồng ( Đơn Vị Tiền Tệ Của Việt Nam, Ngân Hàng Nhà Nước Việt Nam

These two functions get (the first) và emang lại (the second) the image that is mix as "Featured Image" for a post.

What is returned (or echoed) by these functions is a full img html tag, something like

So, which is the kích cỡ used?

If a $kích cỡ is passed as 2nd argument (1st is $postid), then these functions return the image in that size. Otherwise, these functions search for a image size registered with the name: post-thumbnail.

This is not one of the 3 default sizes, in fact, it is the 4th standard kích cỡ (the 5th is considered full) và you can register it calling set_post_thumbnail_kích cỡ.

So

set_post_thumbnail_size( $width, $height, $crop );is a shortcut for

add_image_size( "post-thumbnail", $width, $height, $crop );If you haven"t called set_post_thumbnail_kích thước (which means the kích cỡ "post-thumbnail" is not created), nguyenminhchau.com will use the kích cỡ thumb, & if even this kích thước is not available, nguyenminhchau.com will use the original image, the one not resized.

Registering custom image sizes can be very helpful when designing a theme, but it"s important not lớn abuse: every uploaded image is copied và resized for all the sizes registered, so registering a lot of sizes will heavily slow down the image uploading process.

A note:

Be aware that when nguyenminhchau.com creates scaled copies of an image, it never enlarges it, but only makes smaller copies: e.g. if the original image is 400x500px, and the "medium" image kích thước is 800x600px, medium kích cỡ & larger are not created.

So registering an image kích thước never gives the security that an image tệp tin for every kích thước registered actually exists in the nguyenminhchau.com content thư mục. Even because registered image sizes easily change: changing web1_settings, switching themes, etc.

When an image kích cỡ changes, for any reason, the change has an effect on the images uploaded after that change; images uploaded before don"t change dimensions và are not rescaled và resaved.

Xem thêm: Tổng Hợp Các Cách Nâng Cao Lưu Vào Bộ Nhớ Đệm Trong Trình Duyệt Mới Nhất 2020

If one needs to rely on specific image sizes after some images are already uploaded (e.g. after changing themes), then the plugin Regenerate Thumbnails will be a life saver.


Chuyên mục: Lỗi Kỹ Thuật