Kích thước hình thu nhỏ tùy chỉnh WordPress

Cập nhật ngày 4 tháng 4 năm 2017. Đã thêm phần mô tả chi tiết cách hệ thống hình ảnh phản hồi trong WordPress 4. 4+ hoạt động và cách kích thước hình ảnh tùy chỉnh của WordPress tương tác với hệ thống này

Tôi thực sự thích có thể đặt kích thước hình ảnh tùy chỉnh trong WordPress. Sức mạnh và tính linh hoạt của hệ thống này mở ra cho việc xây dựng các trang web đẹp, dễ quản trị, hoạt động hiệu quả là điều khó có thể phóng đại

Ví dụ: chủ đề WPShout mới ra mắt có hai kích thước hình ảnh nổi bật. cái lớn [ở đây trên đầu bài đăng này] và cái nhỏ hơn ở giữa trang chủ. Chúng tôi cũng sử dụng kích thước hình ảnh tùy chỉnh để đảm bảo rằng hình ảnh trong nội dung bài đăng của chúng tôi không quá lớn so với không gian mà chúng đang chiếm giữ

Đó là rất nhiều sự phức tạp về kích thước hình ảnh và WordPress chỉ xử lý tất cả cho chúng tôi, ngay lập tức. Hãy tưởng tượng nếu chúng tôi tự mình thực hiện tất cả việc thay đổi kích thước và tối ưu hóa, trong một thứ gì đó như Photoshop—thì nó sẽ chỉ ăn tươi nuốt sống công việc kinh doanh của chúng tôi. Hình ảnh sẽ là một thứ xa xỉ không thể đạt được

Với bài nói chuyện đó, hãy xem cách bạn cũng có thể sử dụng các kích thước hình ảnh tùy chỉnh của WordPress

Một điều nữa trước khi chúng ta bắt đầu. Nếu bạn đang muốn học cách phát triển WordPress, chúng tôi đã viết hướng dẫn tốt nhất về nó

Cách tốt nhất để học phát triển WordPress

Hãy đứng dậy và chạy ngay hôm nay

Up and Running là khóa học “tìm hiểu phát triển WordPress” hoàn chỉnh của chúng tôi. Giờ đây, trong Phiên bản thứ ba được cập nhật và mở rộng, nó đã giúp hàng trăm người mua hài lòng tìm hiểu cách phát triển WordPress một cách nhanh chóng, thông minh và kỹ lưỡng

Đây là những gì họ phải nói

“Tôi nghĩ rằng bất kỳ ai quan tâm đến việc học phát triển WordPress CẦN khóa học này. Xem video giống như một loạt đèn được bật. ” -Jason, nhà phát triển WordPress

“Các khóa học khác mà tôi đã thử gần như luôn thiếu giải thích rõ ràng về lý do tại sao WordPress làm mọi thứ theo một cách nhất định hoặc cách mọi thứ hoạt động cùng nhau. Up and Running làm được tất cả những điều này và mọi thứ đều được giải thích rõ ràng và bằng ngôn ngữ dễ hiểu. ” -Caroline, WordPress freelancer

Đứng dậy và chạy ngay

Hướng dẫn rất nhanh về kích thước hình ảnh WordPress

Mỗi khi bạn tải lên một hình ảnh, WordPress sẽ tạo một phiên bản đã thay đổi kích thước của hình ảnh cho từng kích thước hình ảnh tùy chỉnh đã đăng ký

Mỗi khi bạn tải hình ảnh lên trang web WordPress của mình, WordPress sẽ tự động tạo phiên bản đã thay đổi kích thước của hình ảnh đó cho mọi kích thước hình ảnh tùy chỉnh mà chủ đề của bạn [và chủ đề gốc, nếu bạn đang sử dụng chủ đề con] đã đăng ký

Trong hệ thống tệp WordPress của bạn, nơi chứa các tệp hình ảnh đó, trông như thế này

Bạn có thấy sức mạnh của điều này? . Nhưng bạn cũng có thể sử dụng các phiên bản được tạo tự động, thay đổi kích thước và cắt xén của cùng một hình ảnh đó trên trang web của mình. Hình ảnh sẽ vừa với không gian mà nó phù hợp và không phải là một tệp lớn hơn mức cần thiết—và WordPress sẽ tự động xử lý tất cả

Một trường hợp đặc biệt là hình ảnh nổi bật, một hình ảnh duy nhất mà bạn đặt làm “đại diện chính thức” của bài đăng mà nó được đính kèm. WordPress có những cách đặc biệt để hiển thị hình ảnh nổi bật trên trang web và bạn có thể tận dụng chức năng cắt xén và thay đổi kích thước giống như bạn có thể với bất kỳ hình ảnh nào khác

Tất cả nghe có vẻ khá tuyệt, phải không?

“Hình thu nhỏ” và “Hình ảnh nổi bật”. Ugh, chúng ta đi đây

Trước khi chúng tôi bắt đầu viết mã, có một đường vòng ngữ nghĩa WordPress mà chúng tôi sẽ cần thực hiện

“Hình ảnh nổi bật” bắt đầu và thường được gọi là “Hình thu nhỏ. ”

Bởi vì WordPress tương thích ngược, “Hình ảnh nổi bật” bắt đầu và thường được gọi là “Hình thu nhỏ. ” [Loại vấn đề đặt tên kế thừa này nghe có vẻ quen thuộc từ bài đăng trước đây của chúng tôi về “thanh bên” và “khu vực tiện ích con. ”]

Vấn đề này tăng lên như sau. chức năng WordPress


// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
3 đặt kích thước hình ảnh thu nhỏ trên trang web của bạn. Kích thước này [150px vuông theo mặc định] cũng là kích thước hình ảnh nổi bật mặc định. đó là những gì bạn sẽ thấy nếu bạn gọi hàm để hiển thị các hình ảnh nổi bật—

// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
4—không có đối số

Vì vậy, bạn đã có một hàm có tên khó hiểu,


// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
4, với mục đích thực sự là hiển thị các hình ảnh nổi bật. Bạn cũng có một chức năng hoàn toàn kỳ lạ,

// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
3, đặt kích thước “Hình thu nhỏ” mặc định và kích thước hình ảnh nổi bật trên trang web của bạn

Lời khuyên của tôi là chỉ để lại


// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
3 hoàn toàn không sử dụng

Lời khuyên của tôi là hãy để hoàn toàn yên cho


// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
3; . Để các hình ảnh có kích thước “hình thu nhỏ” [hình vuông 150px] lủng lẳng như một phụ lục và để nó ở đó. Thay vào đó, chúng tôi sẽ sử dụng một chức năng có tên là
// Start The Loop
if [ have_posts[] ] : while [ have_posts[] ] : the_post[];
		
		// This could be anything; here we'll just pretend we want the post title
		the_title[];

		// Display post's featured image, in 'featured-image-large' size
		the_post_thumbnail[ 'featured-image-large' ];

		// Continue with everything else in The Loop...
0 và gọi

// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
4 với các đối số để cho nó biết kích thước hình ảnh mà chúng tôi muốn sử dụng cho các hình ảnh nổi bật của mình

Thêm kích thước hình ảnh tùy chỉnh WordPress trong
// Start The Loop
if [ have_posts[] ] : while [ have_posts[] ] : the_post[];
		
		// This could be anything; here we'll just pretend we want the post title
		the_title[];

		// Display post's featured image, in 'featured-image-large' size
		the_post_thumbnail[ 'featured-image-large' ];

		// Continue with everything else in The Loop...
2

Trong phần này, chúng ta sẽ xem mã chính để xử lý hình ảnh nổi bật và kích thước hình ảnh trong WordPress. Mã mẫu chúng tôi sẽ sử dụng bên dưới;


// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}

Bản thân các nhận xét trong mã giải thích khá nhiều điều đang diễn ra, cũng như phần còn lại của bài đăng này;

// Start The Loop
if [ have_posts[] ] : while [ have_posts[] ] : the_post[];
		
		// This could be anything; here we'll just pretend we want the post title
		the_title[];

		// Display post's featured image, in 'featured-image-large' size
		the_post_thumbnail[ 'featured-image-large' ];

		// Continue with everything else in The Loop...
0 đối số

// Start The Loop
if [ have_posts[] ] : while [ have_posts[] ] : the_post[];
		
		// This could be anything; here we'll just pretend we want the post title
		the_title[];

		// Display post's featured image, in 'featured-image-large' size
		the_post_thumbnail[ 'featured-image-large' ];

		// Continue with everything else in The Loop...
0 có bốn đối số

  1. // Start The Loop
    if [ have_posts[] ] : while [ have_posts[] ] : the_post[];
    		
    		// This could be anything; here we'll just pretend we want the post title
    		the_title[];
    
    		// Display post's featured image, in 'featured-image-large' size
    		the_post_thumbnail[ 'featured-image-large' ];
    
    		// Continue with everything else in The Loop...
    6, một chuỗi [bắt buộc]
  2. // Start The Loop
    if [ have_posts[] ] : while [ have_posts[] ] : the_post[];
    		
    		// This could be anything; here we'll just pretend we want the post title
    		the_title[];
    
    		// Display post's featured image, in 'featured-image-large' size
    		the_post_thumbnail[ 'featured-image-large' ];
    
    		// Continue with everything else in The Loop...
    7, một số nguyên [tùy chọn]
  3. // Start The Loop
    if [ have_posts[] ] : while [ have_posts[] ] : the_post[];
    		
    		// This could be anything; here we'll just pretend we want the post title
    		the_title[];
    
    		// Display post's featured image, in 'featured-image-large' size
    		the_post_thumbnail[ 'featured-image-large' ];
    
    		// Continue with everything else in The Loop...
    8, một số nguyên [tùy chọn]
  4. // Start The Loop
    if [ have_posts[] ] : while [ have_posts[] ] : the_post[];
    		
    		// This could be anything; here we'll just pretend we want the post title
    		the_title[];
    
    		// Display post's featured image, in 'featured-image-large' size
    		the_post_thumbnail[ 'featured-image-large' ];
    
    		// Continue with everything else in The Loop...
    9, một giá trị logic [tùy chọn]

// Start The Loop
if [ have_posts[] ] : while [ have_posts[] ] : the_post[];
		
		// This could be anything; here we'll just pretend we want the post title
		the_title[];

		// Display post's featured image, in 'featured-image-large' size
		the_post_thumbnail[ 'featured-image-large' ];

		// Continue with everything else in The Loop...
6 tự giải thích.
// Start The Loop
if [ have_posts[] ] : while [ have_posts[] ] : the_post[];
		
		// This could be anything; here we'll just pretend we want the post title
		the_title[];

		// Display post's featured image, in 'featured-image-large' size
		the_post_thumbnail[ 'featured-image-large' ];

		// Continue with everything else in The Loop...
7 và
// Start The Loop
if [ have_posts[] ] : while [ have_posts[] ] : the_post[];
		
		// This could be anything; here we'll just pretend we want the post title
		the_title[];

		// Display post's featured image, in 'featured-image-large' size
		the_post_thumbnail[ 'featured-image-large' ];

		// Continue with everything else in The Loop...
8 là các giá trị kích thước mục tiêu, tính bằng pixel. Chúng tôi sẽ giải thích cách chúng hoạt động [và cách chúng hoạt động với
// Start The Loop
if [ have_posts[] ] : while [ have_posts[] ] : the_post[];
		
		// This could be anything; here we'll just pretend we want the post title
		the_title[];

		// Display post's featured image, in 'featured-image-large' size
		the_post_thumbnail[ 'featured-image-large' ];

		// Continue with everything else in The Loop...
9] bên dưới

// Start The Loop
if [ have_posts[] ] : while [ have_posts[] ] : the_post[];
		
		// This could be anything; here we'll just pretend we want the post title
		the_title[];

		// Display post's featured image, in 'featured-image-large' size
		the_post_thumbnail[ 'featured-image-large' ];

		// Continue with everything else in The Loop...
44

Tất cả những gì bạn cần biết về chức năng đó là WordPress yêu cầu một số mã soạn sẵn để thêm kích thước hình ảnh tùy chỉnh vào phương thức “Thêm phương tiện”—nơi người dùng trang web có thể sử dụng chúng—và để xác định tên của các kích thước đó trong phần “Thêm . Móc vào móc bộ lọc có tên là

// Start The Loop
if [ have_posts[] ] : while [ have_posts[] ] : the_post[];
		
		// This could be anything; here we'll just pretend we want the post title
		the_title[];

		// Display post's featured image, in 'featured-image-large' size
		the_post_thumbnail[ 'featured-image-large' ];

		// Continue with everything else in The Loop...
45 và đăng ký ba kích thước hình ảnh mong muốn đều là công việc của
// Start The Loop
if [ have_posts[] ] : while [ have_posts[] ] : the_post[];
		
		// This could be anything; here we'll just pretend we want the post title
		the_title[];

		// Display post's featured image, in 'featured-image-large' size
		the_post_thumbnail[ 'featured-image-large' ];

		// Continue with everything else in The Loop...
44;

Sử dụng kích thước hình ảnh đã thêm

Chúng tôi đã nói về


// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
4 trước đó. Nó luôn được gọi trong The Loop. [Nếu bạn ở bên ngoài The Loop, có một chức năng
// Start The Loop
if [ have_posts[] ] : while [ have_posts[] ] : the_post[];
		
		// This could be anything; here we'll just pretend we want the post title
		the_title[];

		// Display post's featured image, in 'featured-image-large' size
		the_post_thumbnail[ 'featured-image-large' ];

		// Continue with everything else in The Loop...
48 cho phép bạn chỉ định bài đăng mà bạn đang nói đến. ]

Chúng tôi sẽ luôn gọi


// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
4 với ít nhất một đối số. tên của kích thước hình ảnh tùy chỉnh để sử dụng

Bởi vì chúng tôi không muốn nhận được “hình thu nhỏ” 150px x 150px thực tế như đã thảo luận ở trên, nên chúng tôi sẽ luôn gọi


// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
4 với ít nhất một đối số. tên của kích thước hình ảnh tùy chỉnh do chúng tôi đăng ký mà chúng tôi muốn sử dụng

Nó trông như sau [mã này thuộc về mẫu trang WordPress như

// Start The Loop
if [ have_posts[] ] : while [ have_posts[] ] : the_post[];
		
		// This could be anything; here we'll just pretend we want the post title
		the_title[];

		// Display post's featured image, in 'featured-image-large' size
		the_post_thumbnail[ 'featured-image-large' ];

		// Continue with everything else in The Loop...
81 hoặc
// Start The Loop
if [ have_posts[] ] : while [ have_posts[] ] : the_post[];
		
		// This could be anything; here we'll just pretend we want the post title
		the_title[];

		// Display post's featured image, in 'featured-image-large' size
		the_post_thumbnail[ 'featured-image-large' ];

		// Continue with everything else in The Loop...
82]

// Start The Loop
if [ have_posts[] ] : while [ have_posts[] ] : the_post[];
		
		// This could be anything; here we'll just pretend we want the post title
		the_title[];

		// Display post's featured image, in 'featured-image-large' size
		the_post_thumbnail[ 'featured-image-large' ];

		// Continue with everything else in The Loop...

Ở đó bạn có nó. Bạn đã đặt hình ảnh nổi bật của bài đăng, ở kích thước hình ảnh xác định, lên trang. Có nhiều thứ để tìm hiểu ở đây, nhưng điều đó sẽ giúp bạn bắt đầu. Nếu bạn có thêm câu hỏi về các đối số chức năng bổ sung, bạn có thể giải quyết trên


// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
4, hãy kiểm tra Codex

Tạo lại hình thu nhỏ

Chạy Tạo lại hình thu nhỏ sau mỗi lần bạn thay đổi kích thước hình ảnh trên trang web của mình

Một trong năm plugin WordPress yêu thích nhất mọi thời đại của tôi là Tạo lại hình thu nhỏ. Nó thực hiện công việc bẩn thỉu là đảm bảo mọi hình ảnh bạn tải lên đều có phiên bản cho tất cả các kích thước hình ảnh bạn khai báo. Chạy nó sau mỗi lần bạn tăng kích thước hình ảnh trên trang web của mình, nếu không những hình ảnh cũ sẽ bắt đầu trông rất buồn cười

Hiểu cắt xén

Hãy xem xét các đối số chức năng của

// Start The Loop
if [ have_posts[] ] : while [ have_posts[] ] : the_post[];
		
		// This could be anything; here we'll just pretend we want the post title
		the_title[];

		// Display post's featured image, in 'featured-image-large' size
		the_post_thumbnail[ 'featured-image-large' ];

		// Continue with everything else in The Loop...
0 kỹ hơn một chút. Nếu bạn để ý, một số lệnh gọi hàm có đối số thứ tư, được đặt thành
// Start The Loop
if [ have_posts[] ] : while [ have_posts[] ] : the_post[];
		
		// This could be anything; here we'll just pretend we want the post title
		the_title[];

		// Display post's featured image, in 'featured-image-large' size
		the_post_thumbnail[ 'featured-image-large' ];

		// Continue with everything else in The Loop...
85 và một số lệnh gọi chỉ có hai hoặc ba. Hóa ra, điều này rất quan trọng đối với hình ảnh của bạn trông như thế nào và là một phần sức mạnh tuyệt vời của kích thước hình ảnh tùy chỉnh WordPress

Chúng tôi sẽ cấu trúc cuộc thảo luận này bằng cách xem xét hai loại cắt xén có sẵn. cây trồng mềm và cây trồng cứng

cây trồng mềm

Thuật ngữ chính xác hơn cho “cắt xén mềm” có thể là “thay đổi kích thước. ”

“Soft crop” thực sự là một cách gọi sai. thuật ngữ chính xác hơn có thể là “thay đổi kích thước. ” Đó là mặc định của WordPress. bạn có thể cắt xén mềm bằng cách bỏ qua đối số chức năng thứ tư trong

// Start The Loop
if [ have_posts[] ] : while [ have_posts[] ] : the_post[];
		
		// This could be anything; here we'll just pretend we want the post title
		the_title[];

		// Display post's featured image, in 'featured-image-large' size
		the_post_thumbnail[ 'featured-image-large' ];

		// Continue with everything else in The Loop...
0 [hoặc

// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
3, nếu bạn quyết định sử dụng nó] hoặc đặt đối số đó thành
// Start The Loop
if [ have_posts[] ] : while [ have_posts[] ] : the_post[];
		
		// This could be anything; here we'll just pretend we want the post title
		the_title[];

		// Display post's featured image, in 'featured-image-large' size
		the_post_thumbnail[ 'featured-image-large' ];

		// Continue with everything else in The Loop...
88

Bất kể, điều cần biết về cắt xén mềm là nó duy trì tỷ lệ hình ảnh của bạn. Nói cách khác, nó sẽ thay đổi kích thước xem toàn bộ hình ảnh theo kích thước bạn chỉ định, nhưng nó sẽ không thay đổi hình dạng của hình ảnh hoặc cắt bỏ bất kỳ phần nào của hình ảnh. Điều này làm cho phần mềm cắt xén trở thành mặc định thông minh cho các kích thước hình ảnh mà bạn hy vọng sẽ sử dụng bên trong nội dung bài đăng và trang của mình

Như một điểm tốt, cây trồng mềm có thể hoạt động theo một trong hai cách. với một hoặc hai kích thước được chỉ định

Với một kích thước được chỉ định

Điều này tương ứng với mã cho kích thước hình ảnh

// Start The Loop
if [ have_posts[] ] : while [ have_posts[] ] : the_post[];
		
		// This could be anything; here we'll just pretend we want the post title
		the_title[];

		// Display post's featured image, in 'featured-image-large' size
		the_post_thumbnail[ 'featured-image-large' ];

		// Continue with everything else in The Loop...
89 và

// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
30 ở trên

Nếu bạn cắt xén mềm và chỉ xác định một chiều [chỉ chiều rộng hoặc chiều cao cộng với “9999” cho chiều rộng], bạn sẽ nhận được hình ảnh tăng tỷ lệ thuận với kích thước của chiều đó. Vì vậy, nếu bạn chỉ định 300 cho chiều rộng, hình ảnh của bạn sẽ rộng 300 pixel và bất kỳ chiều cao nào làm cho hình ảnh có tỷ lệ

Với hai kích thước được chỉ định

Điều này tương ứng với mã cho kích thước hình ảnh


// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
31 ở trên

Nếu bạn cắt mềm và chỉ định cả chiều rộng và chiều cao, hình ảnh sẽ phát triển theo tỷ lệ cho đến khi một trong các kích thước chạm vào mục tiêu của nó và nó sẽ dừng ở đó. Vì vậy, nếu bạn chỉ định chiều rộng 300px và chiều cao 100px, thì một hình ảnh dài sẽ dừng ở 300px x 80px và một hình ảnh cao sẽ dừng ở 60px x 100px

Ba hình ảnh sau đây minh họa kết quả cắt mềm cho ba kích thước hình ảnh được cắt mềm mà chúng tôi đã đăng ký ở trên

Nhấp để phóng to

cây trồng cứng

Điều này tương ứng với mã cho kích thước hình ảnh


// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
32 và

// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
33 ở trên

Về cơ bản, một cây trồng cứng sẽ cắt một thứ gì đó thành hình dạng và kích thước được xác định trước

Bạn đạt được một vụ mùa khó khăn bằng cách đặt tham số thứ tư của

// Start The Loop
if [ have_posts[] ] : while [ have_posts[] ] : the_post[];
		
		// This could be anything; here we'll just pretend we want the post title
		the_title[];

		// Display post's featured image, in 'featured-image-large' size
		the_post_thumbnail[ 'featured-image-large' ];

		// Continue with everything else in The Loop...
0 [hoặc

// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
3] thành
// Start The Loop
if [ have_posts[] ] : while [ have_posts[] ] : the_post[];
		
		// This could be anything; here we'll just pretend we want the post title
		the_title[];

		// Display post's featured image, in 'featured-image-large' size
		the_post_thumbnail[ 'featured-image-large' ];

		// Continue with everything else in The Loop...
85

Nhấp để phóng to

Lưu ý rằng hình ảnh được cắt cứng không còn có hình dạng giống như—cũng như không chứa đầy đủ nội dung của—hình ảnh gốc của nó

Cắt cứng thường hữu ích nhất cho các hình ảnh nổi bật, thường phải vừa với một khu vực xác định trong bố cục trang web của bạn

Theo kinh nghiệm của tôi, cắt xén hữu ích nhất cho các hình ảnh nổi bật, thường phải vừa với một khu vực xác định trong bố cục trang web của bạn. Cũng có những cách sử dụng khác cho nó, nhưng đó là nơi nó thực sự tiện dụng

Bạn có thể sẽ không muốn cắt xén hình ảnh mà bạn sẽ chèn vào bài đăng của mình [trừ khi bạn muốn, chẳng hạn, tất cả hình ảnh của bạn thành hình vuông như một quyết định thiết kế], vì nội dung bài đăng thường được trình bày trôi chảy và cứng cáp hơn.

Điều gì xảy ra nếu Ảnh gốc Nhỏ hơn Kích thước Cắt mong muốn?

Kích thước quá nhỏ sẽ chỉ giữ nguyên kích thước ban đầu của chúng

Câu hỏi hay. Nói chung, nếu hình ảnh bạn tải lên quá nhỏ để đáp ứng kích thước mong muốn, kích thước quá nhỏ sẽ chỉ giữ nguyên kích thước ban đầu của chúng

Giả sử bạn đang cắt xén một hình ảnh thành 500px x 400px, nhưng bản thân hình ảnh gốc thực sự là 300px x 200px. Bạn sẽ chỉ nhận lại phiên bản có kích thước đầy đủ

Cắt mềm

Lưu ý rằng tính năng cắt xén mềm sẽ dừng ngay khi hình ảnh của bạn đạt đến mục tiêu thứ nguyên đầu tiên. Vì vậy, cách duy nhất bạn sẽ không thể cắt xén ở chế độ cắt xén mềm là nếu hình ảnh của bạn quá nhỏ ở cả hai chiều, như ví dụ trong đoạn trước. nếu bạn đang cắt xén mềm thành 500px x 400px và hình ảnh của bạn là 1000px x 200px, thì đây không phải là cắt xén thất bại;

khó cắt

Tuy nhiên, trong chế độ cắt cứng, hình ảnh có thể đủ lớn ở một chiều nhưng lại quá nhỏ ở chiều kia. Nếu bạn chịu khó cắt xén thành 500px x 400px và ảnh gốc là 1000px x 200px, bạn sẽ nhận được hình ảnh có kích thước 500px x 200px. Nó sẽ trông giống như hình ảnh ban đầu, với 50% [500px] ngoài cùng bị cắt từ bên trái và bên phải. Quan trọng nhất, nó sẽ không ở kích thước 5 x 4 mà bạn mong đợi, mà là 5 x 2

…Vì vậy, hãy tải lên những hình ảnh lớn

Như bạn có thể tưởng tượng, điều này có thể gây ra một số định dạng xấu nếu, ví dụ, các hình ảnh nổi bật đột nhiên quá nhỏ [và/hoặc hình dạng sai] đối với vùng chứa của chúng. Vì vậy, hãy tải lên thứ lớn nhất mà bạn có—trong trường hợp khẩn cấp, bạn thậm chí có thể phóng to hình ảnh để đáp ứng yêu cầu của mình bằng một thứ gì đó như Photoshop;

Đặt kích thước hình ảnh phù hợp

Chúng tôi không muốn đặt hình ảnh quá lớn trên trang, vì vậy đây là nơi chúng tôi cần thực hành về việc giới hạn kích thước và kích thước tệp của hình ảnh

Được rồi, bây giờ chúng ta biết cách đặt kích thước hình ảnh WordPress. Bây giờ chúng ta nên đặt kích thước hình ảnh nào?

Bạn có thể nhớ lại bài đăng trước đây của chúng tôi, Commonsense Image Sizing, rằng add_image_size[] cho phép bạn xem mọi hình ảnh ở bất kỳ kích thước nào bạn muốn. Do đó, tốt nhất là bạn nên có nguyên liệu thô lớn nhất để làm việc, vì bạn có thể thu nhỏ nó thành bất kỳ kích thước và kích thước tệp nào bạn muốn

Tuy nhiên, để nói rõ ràng, chúng tôi không muốn đặt hình ảnh quá lớn trên trang vì chúng sẽ làm chậm trang web. Vì vậy, đây là nơi chúng ta cần thực hành về việc giới hạn kích thước và kích thước tệp của hình ảnh. May mắn thay, nó không khó. như trong Commonsense Image Sizing, hướng dẫn là đặt kích thước hình ảnh sẽ là

  1. Đủ lớn để có kích thước phù hợp dưới bất kỳ chế độ xem nào của trang web trên mọi thiết bị
  2. Không lớn hơn thế

Trên thực tế, điều này có nghĩa là chỉ xem trang web của bạn trên các độ rộng thiết bị khác nhau. Nếu một hình ảnh lấp đầy toàn bộ một cột, thì cột đó có thể rộng nhất là bao nhiêu? . Nếu bạn có nhiều hình ảnh sẽ lấp đầy cột đó, bạn sẽ muốn đăng ký kích thước hình ảnh rộng từ 800px trở lên

Điều này có thể trở nên phức tạp nếu bạn bắt đầu nghĩ về hình nền có chiều rộng đầy đủ trên màn hình iMac 14 megapixel. [Ngoài ra, đừng để tôi bắt đầu trên màn hình retina. ] Nhưng đối với hầu hết các hình ảnh [và hầu hết các trang web], chỉ cần giữ cho hình ảnh đơn giản, xem vị trí của một hình ảnh phù hợp trên trang ở các độ rộng khác nhau và tạo kích thước hình ảnh lớn như vậy và không lớn hơn

Hình ảnh đáp ứng của WordPress và cách chúng tương tác với kích thước hình ảnh tùy chỉnh của WordPress

Một cải tiến lớn đối với kích thước hình ảnh WordPress đã xuất hiện vào cuối năm 2015, với WordPress 4. 4. hình ảnh đáp ứng. Đây là một tính năng nền mà bạn không cần phải “làm” bất cứ điều gì để tận hưởng, vì vậy, trong hầu hết các trường hợp, tốt nhất là hãy vui vì những người khác đã tìm ra nó cho chúng ta. Nhưng thật tốt khi hiểu cách nó tương tác với hệ thống kích thước hình ảnh của WordPress

Hình ảnh phản hồi nào tốt cho

Hình ảnh phản hồi tăng tốc trang web cho người dùng trên các thiết bị hẹp, bằng cách cung cấp các kích thước hình ảnh khác nhau cho các cửa sổ trình duyệt có độ rộng khác nhau

Hình ảnh đáp ứng là một quy ước web xuất hiện từ một vấn đề đơn giản. thiết bị hẹp được phục vụ hình ảnh rộng

Nếu bạn đang sử dụng điện thoại di động có chiều rộng 480px, tại sao bạn phải tải hình ảnh [ví dụ: hình ảnh nổi bật của bài đăng] có chiều rộng gấp đôi chiều rộng đó trở lên? . Khi một thiết bị di động tải các hình ảnh lớn chỉ để thay đổi kích thước của chúng theo chiều rộng mà thiết bị có thể xử lý, nó sẽ làm chậm quá trình tải trang—đặc biệt là vấn đề trên điện thoại thường có kết nối tương đối chậm và không ổn định

Mặt khác, chúng tôi không thể chỉ giới hạn mọi hình ảnh trên internet theo chiều rộng của điện thoại hẹp nhất. Điều chúng tôi cần là phân phát các kích thước hình ảnh khác nhau cho các thiết bị khác nhau, dựa trên chiều rộng của thiết bị. Đây là tất cả những gì về hình ảnh phản hồi

Hình ảnh phản hồi hoạt động như thế nào

Đôi khi, bạn có thể đã mở trình kiểm tra mã trình duyệt của mình và cảm thấy sợ hãi trước thẻ


// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
37 trông như thế này

// Start The Loop
if [ have_posts[] ] : while [ have_posts[] ] : the_post[];
		
		// This could be anything; here we'll just pretend we want the post title
		the_title[];

		// Display post's featured image, in 'featured-image-large' size
		the_post_thumbnail[ 'featured-image-large' ];

		// Continue with everything else in The Loop...
4

Đây là đánh dấu cho hình ảnh phản hồi của WordPress. Để hiểu cách chúng hoạt động, hãy tách ví dụ này thành các bộ phận cấu thành của nó


// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
38,

// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
39 và
// Start The Loop
if [ have_posts[] ] : while [ have_posts[] ] : the_post[];
		
		// This could be anything; here we'll just pretend we want the post title
		the_title[];

		// Display post's featured image, in 'featured-image-large' size
		the_post_thumbnail[ 'featured-image-large' ];

		// Continue with everything else in The Loop...
7. Điều gì không thay đổi


// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
41,

// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
42 và

// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
43 không có gì mới đối với hình ảnh phản hồi. Các bit đánh dấu này là cách WordPress gán các lớp, thẻ

// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
39 và các giá trị chiều rộng và chiều cao được mã hóa cứng [gần như hoàn toàn lỗi thời] cho hình ảnh, cho dù chúng có phải là hình ảnh phản hồi của WordPress hay không


// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
45. Bây giờ là dự phòng

Điều này sẽ trông quen thuộc, vì đó là cách hình ảnh không phản hồi hoạt động. chúng có một thuộc tính duy nhất là


// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
45 [“nguồn”], một URL cho biết nơi có thể tìm thấy hình ảnh

Đối với hình ảnh phản hồi,


// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
45 là dự phòng, một cách để làm cho hình ảnh hoạt động cho trình duyệt [xin chào IE. ] không hỗ trợ hình ảnh phản hồi. Vì vậy, những gì trong

// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
45 là những gì sẽ được phục vụ trong IE [và bất kỳ trình duyệt lỗi thời nào khác mà người dùng có thể truy cập trang web, mặc dù tất cả các trình duyệt phổ biến khác ngoài IE đều hỗ trợ hình ảnh phản hồi]


// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
49. một tập hợp các nguồn hình ảnh có kích thước khác nhau

Đây là công cụ mới và là công cụ chính của hình ảnh phản hồi.


// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
49 xác định danh sách các thuộc tính

// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
45 [một “bộ nguồn”] mà trình duyệt sẽ tìm nạp, tùy thuộc vào độ rộng của nó. Hãy xem xét tùy chọn đầu tiên của

// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
49


// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
43

Điều này thực sự được tạo thành từ hai phần, được ngăn cách bởi một khoảng trắng

  1. 
    // Make sure featured images are enabled
    add_theme_support[ 'post-thumbnails' ];
    
    // Add featured image sizes
    add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
    add_image_size[ 'featured-small', 320, 147, true ];
    
    // Add other useful image sizes for use through Add Media modal
    add_image_size[ 'medium-width', 480 ];
    add_image_size[ 'medium-height', 9999, 480 ];
    add_image_size[ 'medium-something', 480, 480 ];
    
    // Register the three useful image sizes for use in Add Media modal
    add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
    function wpshout_custom_sizes[ $sizes ] {
        return array_merge[ $sizes, array[
            'medium-width' => __[ 'Medium Width' ],
            'medium-height' => __[ 'Medium Height' ],
            'medium-something' => __[ 'Medium Something' ],
        ] ];
    }
    44. Đây là liên kết hình ảnh, thứ mà bạn có thể dán vào trình duyệt của mình [thử nó. ] để xem một hình ảnh. Nếu liên kết hình ảnh này có vẻ buồn cười với bạn, thì đó là vì WPShout đang sử dụng CDN [mạng phân phối nội dung] để phân phối hình ảnh của nó. Trang web của riêng bạn liên kết của tôi trông giống như chỉ đơn giản là
    
    // Make sure featured images are enabled
    add_theme_support[ 'post-thumbnails' ];
    
    // Add featured image sizes
    add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
    add_image_size[ 'featured-small', 320, 147, true ];
    
    // Add other useful image sizes for use through Add Media modal
    add_image_size[ 'medium-width', 480 ];
    add_image_size[ 'medium-height', 9999, 480 ];
    add_image_size[ 'medium-something', 480, 480 ];
    
    // Register the three useful image sizes for use in Add Media modal
    add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
    function wpshout_custom_sizes[ $sizes ] {
        return array_merge[ $sizes, array[
            'medium-width' => __[ 'Medium Width' ],
            'medium-height' => __[ 'Medium Height' ],
            'medium-something' => __[ 'Medium Something' ],
        ] ];
    }
    45, v.v.
  2. 
    // Make sure featured images are enabled
    add_theme_support[ 'post-thumbnails' ];
    
    // Add featured image sizes
    add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
    add_image_size[ 'featured-small', 320, 147, true ];
    
    // Add other useful image sizes for use through Add Media modal
    add_image_size[ 'medium-width', 480 ];
    add_image_size[ 'medium-height', 9999, 480 ];
    add_image_size[ 'medium-something', 480, 480 ];
    
    // Register the three useful image sizes for use in Add Media modal
    add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
    function wpshout_custom_sizes[ $sizes ] {
        return array_merge[ $sizes, array[
            'medium-width' => __[ 'Medium Width' ],
            'medium-height' => __[ 'Medium Height' ],
            'medium-something' => __[ 'Medium Something' ],
        ] ];
    }
    46. Đây là những gì quy định chiều rộng mục tiêu mà hình ảnh này sẽ áp dụng. nói cách khác,
    
    // Make sure featured images are enabled
    add_theme_support[ 'post-thumbnails' ];
    
    // Add featured image sizes
    add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
    add_image_size[ 'featured-small', 320, 147, true ];
    
    // Add other useful image sizes for use through Add Media modal
    add_image_size[ 'medium-width', 480 ];
    add_image_size[ 'medium-height', 9999, 480 ];
    add_image_size[ 'medium-something', 480, 480 ];
    
    // Register the three useful image sizes for use in Add Media modal
    add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
    function wpshout_custom_sizes[ $sizes ] {
        return array_merge[ $sizes, array[
            'medium-width' => __[ 'Medium Width' ],
            'medium-height' => __[ 'Medium Height' ],
            'medium-something' => __[ 'Medium Something' ],
        ] ];
    }
    46 có nghĩa là “nguồn hình ảnh này áp dụng cho chiều rộng là 1024 pixel rộng hoặc hẹp hơn. ”

Nhưng "áp dụng cho chiều rộng" nghĩa là gì?


// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
48. Quy tắc chỉ định thời điểm hiển thị hình ảnh nào từ

// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
49


// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
48 hoạt động cùng với

// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
49 như một thành phần quan trọng của hình ảnh phản hồi.

// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
48 là một tập hợp các câu lệnh về độ rộng của trình duyệt để kiểm tra lần lượt từng câu lệnh, với mỗi câu lệnh được ghép nối với một quy tắc để tuân theo nếu câu lệnh về độ rộng của trình duyệt đó là đúng. [Đừng lo lắng, vài đoạn tiếp theo sẽ làm rõ điều này. ]

Thuộc tính


// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
48 của hình ảnh phản hồi của chúng tôi đọc như sau


// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
35

Đọc bằng tiếng Anh, sẽ tuân theo hai quy tắc này

  1. “Nếu trình duyệt rộng 1.000 pixel hoặc hẹp hơn, hãy sử dụng hình ảnh nhỏ nhất từ ​​
    
    // Make sure featured images are enabled
    add_theme_support[ 'post-thumbnails' ];
    
    // Add featured image sizes
    add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
    add_image_size[ 'featured-small', 320, 147, true ];
    
    // Add other useful image sizes for use through Add Media modal
    add_image_size[ 'medium-width', 480 ];
    add_image_size[ 'medium-height', 9999, 480 ];
    add_image_size[ 'medium-something', 480, 480 ];
    
    // Register the three useful image sizes for use in Add Media modal
    add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
    function wpshout_custom_sizes[ $sizes ] {
        return array_merge[ $sizes, array[
            'medium-width' => __[ 'Medium Width' ],
            'medium-height' => __[ 'Medium Height' ],
            'medium-something' => __[ 'Medium Something' ],
        ] ];
    }
    49 có ít nhất 100% chiều rộng của trình duyệt. ”
  2. “Nếu không, hãy sử dụng hình ảnh nhỏ nhất có chiều rộng ít nhất 1.000 pixel. ”

Vì vậy,


// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
48 đi theo thứ tự từ câu lệnh chiều rộng đầu tiên đến câu lệnh cuối cùng, dừng ở câu lệnh đúng đầu tiên. Trong trường hợp của WPShout, chỉ có hai quy tắc trong

// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
48. Một cái giữ nếu trình duyệt rộng hoặc hẹp hơn

// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
39 [trong trường hợp đó, hình ảnh nhỏ nhất từ ​​

// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
49 có chiều rộng

// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
31 hoặc rộng hơn sẽ được sử dụng] và một cái giữ ở mọi thời điểm khác [trong trường hợp đó, hình ảnh nhỏ nhất từ ​​

// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
49 là

// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
39 hoặc

Chỉ định Quy tắc trong

// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
48 với

// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
35

Xin lưu ý, thiết lập


// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
48 hai quy tắc của WPShout, tập trung xung quanh

// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
39 làm điểm dừng, là mặc định cho các hình ảnh phản hồi của WordPress, vì chúng tôi tại WPShout không bao giờ bận tâm đến việc chỉ định các quy tắc thông minh hơn. Một số chủ đề khác, đặc biệt là các chủ đề “chính thức” như Twenty Sixteen, sử dụng hook bộ lọc WordPress có tên là

// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
35 để đặt các giá trị

// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
48 thông minh hơn và theo chủ đề cụ thể hơn. Đối với Twenty Sixteen, thuộc tính

// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
48 đọc như sau


// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
31

Nếu bạn muốn hiểu sâu hơn về


// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
49 và

// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
48, chúng đã được giải thích cặn kẽ tại đây và những nơi khác. Và Tạp chí Smashing đã hoàn thành tốt công việc giải thích cách sử dụng

// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
35 để đặt các câu lệnh về độ rộng, điểm dừng và quy tắc của riêng bạn trong

// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
48 cho hình ảnh phản hồi của WordPress

Cách hình ảnh đáp ứng của WordPress tương tác với kích thước hình ảnh tùy chỉnh

Hệ thống hình ảnh phản hồi của WordPress tương tác khá đơn giản và lành mạnh với kích thước hình ảnh tùy chỉnh

Trong một thời gian dài, tôi đã khá lo lắng rằng việc tạo kích thước hình ảnh tùy chỉnh của riêng mình bằng cách nào đó có thể “hỏng”, làm chậm hoặc ảnh hưởng đến kích thước hình ảnh phản hồi của WordPress [điều mà tôi không hiểu rõ]. Tôi vui mừng báo cáo rằng cách hai hệ thống này tương tác với nhau khá đơn giản và lành mạnh

  1. Mọi kích thước hình ảnh tùy chỉnh mà bạn tạo có cùng tỷ lệ khung hình [tỷ lệ chiều rộng-chiều cao] như hình ảnh gốc sẽ chỉ được thêm vào
    
    // Make sure featured images are enabled
    add_theme_support[ 'post-thumbnails' ];
    
    // Add featured image sizes
    add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
    add_image_size[ 'featured-small', 320, 147, true ];
    
    // Add other useful image sizes for use through Add Media modal
    add_image_size[ 'medium-width', 480 ];
    add_image_size[ 'medium-height', 9999, 480 ];
    add_image_size[ 'medium-something', 480, 480 ];
    
    // Register the three useful image sizes for use in Add Media modal
    add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
    function wpshout_custom_sizes[ $sizes ] {
        return array_merge[ $sizes, array[
            'medium-width' => __[ 'Medium Width' ],
            'medium-height' => __[ 'Medium Height' ],
            'medium-something' => __[ 'Medium Something' ],
        ] ];
    }
    49
  2. Mọi kích thước hình ảnh tùy chỉnh được cắt cứng mà bạn tạo với tỷ lệ khung hình khác với hình ảnh gốc sẽ bị hệ thống hình ảnh phản hồi của WordPress bỏ qua

Để cho bạn thấy cách thức hoạt động của tính năng này, tôi đã chụp hai ảnh chụp màn hình theo thứ tự trên một chủ đề là chủ đề con của Twenty Sixteen và do đó kế thừa các quy tắc


// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
48 của nó. Đây là ảnh chụp màn hình đầu tiên, trước khi tạo bất kỳ kích thước hình ảnh tùy chỉnh nào trong chủ đề con

Nhấp để phóng to

Sau đó, tôi đã tạo hai kích thước hình ảnh tùy chỉnh trong

// Start The Loop
if [ have_posts[] ] : while [ have_posts[] ] : the_post[];
		
		// This could be anything; here we'll just pretend we want the post title
		the_title[];

		// Display post's featured image, in 'featured-image-large' size
		the_post_thumbnail[ 'featured-image-large' ];

		// Continue with everything else in The Loop...
3 của chủ đề con bằng mã sau

// Start The Loop
if [ have_posts[] ] : while [ have_posts[] ] : the_post[];
		
		// This could be anything; here we'll just pretend we want the post title
		the_title[];

		// Display post's featured image, in 'featured-image-large' size
		the_post_thumbnail[ 'featured-image-large' ];

		// Continue with everything else in The Loop...
8

Tiếp theo mình reup ảnh và chụp màn hình như sau

Nhấp để phóng to

Như bạn có thể thấy,


// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
49 có một kích thước mới. phiên bản 1600px-by-900px của hình ảnh bên dưới. Bởi vì ảnh cắt cứng 2000px x 1500px không có cùng tỷ lệ khung hình [4. 3 thay vì 16. 9], nó không được thêm vào

// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
49

Bây giờ, phiên bản 1600px x 900px của hình ảnh có bao giờ được hiển thị cho bất kỳ người dùng nào trên bất kỳ thiết bị nào không? . Hóa ra, nó không phải là. quy tắc cuối cùng, bao gồm bất kỳ thiết bị nào rộng hơn 1362px, tìm phần tử nhỏ nhất trong


// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
49 rộng hơn 840px và đó luôn là phần tử

// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
46 [rộng 1024px] thay vì phần tử
// Start The Loop
if [ have_posts[] ] : while [ have_posts[] ] : the_post[];
		
		// This could be anything; here we'll just pretend we want the post title
		the_title[];

		// Display post's featured image, in 'featured-image-large' size
		the_post_thumbnail[ 'featured-image-large' ];

		// Continue with everything else in The Loop...
04. Vì vậy, trong trường hợp này, kích thước hình ảnh tùy chỉnh cụ thể mà chúng tôi đã đăng ký không phải là một bổ sung hữu ích cho

// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
49, vì luôn có một kích thước khác phù hợp hơn với các quy tắc của chủ đề

// Make sure featured images are enabled
add_theme_support[ 'post-thumbnails' ];

// Add featured image sizes
add_image_size[ 'featured-large', 640, 294, true ]; // width, height, crop
add_image_size[ 'featured-small', 320, 147, true ];

// Add other useful image sizes for use through Add Media modal
add_image_size[ 'medium-width', 480 ];
add_image_size[ 'medium-height', 9999, 480 ];
add_image_size[ 'medium-something', 480, 480 ];

// Register the three useful image sizes for use in Add Media modal
add_filter[ 'image_size_names_choose', 'wpshout_custom_sizes' ];
function wpshout_custom_sizes[ $sizes ] {
    return array_merge[ $sizes, array[
        'medium-width' => __[ 'Medium Width' ],
        'medium-height' => __[ 'Medium Height' ],
        'medium-something' => __[ 'Medium Something' ],
    ] ];
}
48, nhưng nó cũng không gây hại gì.

Vui thích

Như tôi đã nói, API kích thước hình ảnh là một điều thực sự tuyệt vời về WordPress. Tôi hy vọng điều này đã giúp bạn hiểu được cái gì, cách thức và lý do tạo kích thước hình ảnh tùy chỉnh và kích thước hình ảnh nổi bật của WordPress

Vì vậy, những thủ thuật nào bạn biết để định cỡ hình ảnh trong WordPress?

P. S. bạn có muốn tìm hiểu thêm về phát triển WordPress không? . Bắt đầu học ngay hôm nay

Kích thước hình thu nhỏ tốt cho WordPress là gì?

Hình nền phải có kích thước 1920 x 1080 pixel. Hình ảnh biểu trưng phải là 200 x 100 pixel. Hình ảnh thu nhỏ phải được đặt thành 150 x 150 pixel .

Làm cách nào để sử dụng Add_image_size trong WordPress?

Để sử dụng kích thước hình ảnh tùy chỉnh cho hình ảnh nổi bật của bài đăng, bạn có thể sử dụng the_post_thumbnail[] trong tệp mẫu chủ đề thích hợp … Lưu ý. Để bật hình ảnh nổi bật, chủ đề hiện tại phải bao gồm add_theme_support[ 'post-thumbnails' ]; . tập tin php. Xem thêm Hình thu nhỏ bài đăng.

Kích thước tiêu chuẩn của một hình ảnh thu nhỏ là gì?

Kích thước hình thu nhỏ lý tưởng là 1280 × 720 pixel với chiều rộng tối thiểu là 640 pixel và tỷ lệ lý tưởng cho trình phát và bản xem trước trên YouTube là . 9.

Chủ Đề