Post thumbnails « wordpress codex


When you upload a picture lớn your WordPress website, its copies – images with additional sizes will be created. Why bởi we need them? Because we don’t use the same kích cỡ image everywhere on the site:

in posts,in portfolio,in popups,as post thumbnails on archive sầu pages,in widgets,in administration area etc;

So, for example WordPress allows you to use full kích cỡ image on posts pages and 150×150 (for example) images on archive pages.

Bạn đang xem: Post thumbnails « wordpress codex

But wait ✋ – keep in mind that the more registered image sizes you have sầu, the more files will be on your website and more time will be needed to lớn process images during upload.

Default WordPress Image Sizes

WordPress supports several image sizes by mặc định. You can find & change them in administration area in "Settings > Media".


As you can see, it is possible to lớn add you custom kích thước there:

add_filter("image_size_names_choose", "misha_new_image_sizes");function misha_new_image_sizes($sizes) $addsizes = array("my-image-size-name" => "Misha size" // "image size name" => "Label (anything)");$web1_newsizes = array_merge( $sizes, $addsizes );return $web1_newsizes;
The cool thing is that this hook also works for Gutenberg image blochồng.

How lớn Regenerate Images after Changing their Sizes or Changing a Theme

Perhaps you know the situation when after changing add_image_size() parameters or after changing your website theme WordPress does not generate image sizes automatically và images with old resolutions are still displayed on the site.

Xem thêm: Cách Lấy Dữ Liệu Từ Webservice, Android Kotlin Căn Bản 8

What should you vì chưng in this situation? If there are only 2-3 pictures in your uploads folder, you can simply reupload them. But what khổng lồ vị if there are 2-3 thousands of images?

There is two ways khổng lồ solve this situation – with plugins & with code:

Regenerate Thumbnails with Plugins

Ok, I can recommend you two simple plugins:

Ajax Thumbnail RebuildForce Regenerate Thumbnail

Each of them has its own advantages and disadvantages, so maybe for different situations you can choose what plugin more fits for you.

Regenerate Thumbnails Programmatically

Here I just want to lớn show you the code which allows to lớn regenerate images, how you will use it – it is your choice.

$attachment_id = 5; // pass your attachment ID here$path = get_attached_file( $attachment_id );$meta = wp_generate_attachment_metadata( $attachment_id, $path );wp_update_attachment_metadata( $attachment_id, $meta );

How to Create Certain Image Size Thumbnails for Custom Post Types Only

So, let’s imagine that your trang web has 10 registered custom post types, and each of them use 2-3 image sizes on the trang web pages. It is easy to underst& that when we upload any picture on the trang web WordPress creates 20-30 copies of it!

There is no way to use add_image_size() for a custom post type but here is a small piece of code that solves this problem. This code tells WordPress when it have to create a certain resolution copy of the picture and when it doesn’t.

Both intermediate_image_sizes và intermediate_image_sizes_advanced hooks are OK for this task. A super simple example below, after you insert the code lớn your functions.php file thumbnails my-image-size-name won’t be created for images, uploaded from custom post type page.

add_filter( "intermediate_image_sizes_advanced", function( $sizes )if( isset( $_REQUEST<"post_id"> ) && "page" == get_post_type($_REQUEST<"post_id"> ) ) unset( $sizes<"my-image-size-name"> );return $sizes; );
A little bit more complicated example, but it is also correct:

/* * this hook will be fired while you uploading a picture */add_filter( "intermediate_image_sizes", "misha_reduce_image_sizes" );function misha_reduce_image_sizes( $sizes )/* * $sizes - all image sizes array Array ( <0> => thumbnail <1> => medium <2> => large <3> => post-thumbnail ) * get_post_type() lớn get post type */$type = get_post_type($_REQUEST<"post_id">); // $_REQUEST<"post_id"> post id the image uploads toforeach( $sizes as $key => $value )/* * use switch if there are a lot of post types */if( $type == "page" ) if( $value == "regionfeatured" ) // turn off "regionfeatured" for pages unset( $sizes<$key> ); else if ( $type == "region" ) if( !in_array( $value, array("regionfeatured","misha335") ) ) // for regions turn off everyting except "regionfeatured" and "misha335" unset( $sizes<$key> ); else if( $value != "thumbnail" ) // turn off everything except thumbnail unset( $sizes<$key> ); return $sizes;

Related Posts



I love WordPress, WooCommerce and Gutenberg so much. 12yrsofexperience.