最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Create thumbnail size only for a few images?

programmeradmin1浏览0评论

I have a few posts that require their thumbnail sizes to be 1000x430. If I register this size and regenerate the thumbnails, it will create this size for all images in the media folder, if I'm not mistaken. If that's the case, that would be way too many 1000x430 images I don't need. Is there a way to only create this thumbnail size for the images I want?

I have a few posts that require their thumbnail sizes to be 1000x430. If I register this size and regenerate the thumbnails, it will create this size for all images in the media folder, if I'm not mistaken. If that's the case, that would be way too many 1000x430 images I don't need. Is there a way to only create this thumbnail size for the images I want?

Share Improve this question asked Apr 22, 2019 at 22:59 DesiDesi 1,2494 gold badges37 silver badges54 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Yes, you can choose to regenerate the thumbnails for specific images. You have 2 options.

1- Create a simple plugin

Generating thumbnails is done via the wp_generate_attachment_metadata() function, which you can use to generate thumbnails manually. To do so, make a simple plugin that loops through a couple of attachment IDs and generates thumbnail for them. Here's what you need:

<?php
/**
 * Plugin Name: Generate Thumbnails
 * Plugin URI: http://example/
 * Description: A plugin to generate some thumbnails.
 * Version: 1.0
 * Author: Desi
 * Author URI: http://example
 * Text Domain: desi
 *
 */

// Trigger our function once the plugin has been activated
register_activation_hook( __FILE__, 'wpse335041_regenerate_thumbnails' );

function wpse335041_regenerate_thumbnails(){
    // This is an array of attachment ids
    $attachment_ids = [ 14, 490, 77, 129 ];

    // Loop through the ids and generate thumbnail for each
    foreach ( $attachment_ids as $id ) {

        // Check if the attachment is an image
        if( wp_attachment_is_image( $id ) ){
            // Get the attachment path
            $attachment = get_attached_file( $id );

            if( $attachment ) {
                // Generate thumbnails and metadata
                wp_generate_attachment_metadata( $id, $attachment );
            }
        }

    }
}

All you have to do now is to activate the plugin. If there are lots of images that need regeneration, it might take a while.

2- Use a plugin

There's a well-known plugin in the plugin repository that can be used to regenerate the thumbnails for a single or multiple image. You can download the plugin via the plugin repository here. Notice that I'm not the plugin's author.

发布评论

评论列表(0)

  1. 暂无评论