問題描述

我擔心新增新的影像大小時建立的檔案太多。

所有上傳的檔案都會生成新的大小,我需要一個特定的影像大小,只能將該檔案設定為 Fetured 的影像,有沒有辦法呢?

縮圖,媒體等都可以,但是不需要為每個檔案建立新的大小。

我認為這應該在設定精選影像後立即工作。

最佳解決方案

Research

問題 Filter For Featured Image 導致這個答案:How to hook update_post_meta and delete_post_meta?

加上一個很好的全域性變數 ($_wp_additional_image_sizes),在這裡顯示:How to get a list of all the possible thumbnail sizes set within a theme,帶給我們的程式碼,捕捉 「用作特色影像」 動作點選。


Code

這個程式碼在每個 「使用為特色影像」 點選時觸發。然後透過所有其他影像大小並刪除它們,保留預設的 WordPress(縮圖,中等,大) 。在進行 LIVE 之前,請仔細測試此程式碼。 GPL 程式碼。不保證。檢查評論。

/**
    DRAWBACK
    - Setting the Featured Image can be done ONLY ONCE
    -- When first setting the FI, all other attachments intermediate sizes will be deleted
    -- If swapping the FI, the first image's remaining intermediate will be deleted, and the second DON'T HAVE any additional intermediates!

    TODO: Restoring deleted intermediates
    - this post may have an answer: https://wordpress.stackexchange.com/a/8082/12615
*/

add_action( 'added_post_meta', 'wpse_57369_selective_delete_intermediates', 10, 4 );
add_action( 'updated_post_meta', 'wpse_57369_selective_delete_intermediates', 10, 4 );

/**
 * Catches the "Used as featured image" action
*/
function wpse_57369_selective_delete_intermediates( $meta_id, $post_id, $meta_key, $meta_value )
{
    if ( '_thumbnail_id' == $meta_key )
    {
        global $_wp_additional_image_sizes;

       /**
        * The global holds all additional image sizes and contains this:
        *
           array(
           ['post-thumbnail'] => array(
               ['width'] => 1000
               ['height'] => 288
               ['crop'] => 1
           )
           ['large-feature'] => array(
               ['width'] => 1000
               ['height'] => 288
               ['crop'] => 1
           )
           ['small-feature'] => array(
               ['width'] => 500
               ['height'] => 300
               ['crop'] =>
           )
        )
       */

        // Populate a new array with single values based on the keys of the global
        $all_sizes = array();
        foreach ( $_wp_additional_image_sizes as $key => $value )
        {
            $all_sizes[] = $key;
        }

        // Retrieve all attachments of current post/page/cpt
        $all_attachs = get_children( array(
                'post_parent' => $post_id,
                'post_type' => 'attachment',
                'numberposts' => -1,
                'post_mime_type' => 'image'
            ));

        // Determine upload path
        $uploads   = wp_upload_dir();
        $path_pos  = strpos( $uploads['basedir'], 'wp-content/' ); // start position of the string
        $base_path = substr( $uploads['basedir'], 0, $path_pos);  // path before wp-content, e.g., /etc/public_html/

        // Loop through all attachments
        foreach ( $all_attachs as $key => $value )
        {
            // This is the featured image
            if ( $key == $meta_value)
            {
                wpse_57369_delete_files( $key, $all_sizes, $base_path, 'small-feature' );
            }
            // Rest of attached images
            else
            {
                wpse_57369_delete_files( $key, $all_sizes, $base_path, false );
            }
        }
    }
}

/**
 * Delete all custom intermediates files, except when $keep_size is defined
*/
function wpse_57369_delete_files( $ID, $all_sizes, $base_path, $keep_size=false )
{
    foreach ( $all_sizes as $intermediate )
    {
        /* We need to know the image url [0] and if it exists [3] */
        $the_url = wp_get_attachment_image_src( $ID, $intermediate );

        /* If additional image exist, go ahead */
        if( $the_url[3] )
        {
            // Path of the image to be deleted
            $url_pos  = strpos( $the_url[0], 'wp-content/' );
            $url_end  = substr( $the_url[0], $url_pos);

            // Delete all intermediates
            if ( !$keep_size )
            {
                // loga( $ID . ' - ' . $intermediate, 'delete-me');
                unlink( $base_path . $url_end );
            }

            // Featured image, Selective delete
            else
            {
                // Delete intermediate
                if ( $intermediate != $keep_size )
                {
                    // loga( $ID . ' - ' . $intermediate, 'delete-me');
                    unlink( $base_path . $url_end );
                }

                // Keep intermediate, no action needed
                // PROBABLY, RESTORING AN INEXISTENT IMAGE SIZE MUST BE DONE HERE
                else
                {
                    // loga( $ID . ' - ' . $intermediate, 'keep-me');
                }
            }
        }
    }
}

function loga()
{
    // This is the FireBug FirePHP console call
    // http://www.firephp.org/HQ/Use.html
}

Result

上傳影像後的資料夾內容,沒有設定功能影像

fondo-restauraciones 設定為精選影像後的資料夾內容


其他筆記

要處理所有額外的影像大小 (WordPress 預設值和自定義) 使用:

$all_sizes = get_intermediate_image_sizes();

/**
 * $all_images contains all intermediate image sizes, WordPress default and declared custom sizes:
 *
    array(
        [0] => 'thumbnail'
        [1] => 'medium'
        [2] => 'large'
        [3] => 'post-thumbnail'
        [4] => 'large-feature'
        [5] => 'small-feature'
    )
*/

次佳解決方案

您可以透過在核心功能 ($post,應該使用 global $post 在前面呼叫) 中使用以下引數來獲取功能影像 src /source:

wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );

How-to 使用它

我寫了一個免費的外掛 available on GitHub,稱為 「動態影像調整大小」 。

你可以下載& 免費使用它

短程式碼

[dynamic_image]放在您的內容中。短碼有四個引數:

  • src 上傳目錄或 ID 中影像的完整路徑

  • width 整數值

  • height 整數值

  • classes Css 類 – 由空格分隔

… 但也有一個:

模板標籤

global $post;
// Use the template tag with ID *OR* full path
dynamic_image_resize( array(
     // The full path to the image in your uploads folder
     'src'     => wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
     // OR: the ID
     'src'     => get_post_thumbnail_id( $post->ID )

    ,'width'   => 60
    ,'height'  => 100
    ,'classes' => 'some classes to align and style the image'
) );

只需將其轉儲到您需要的模板中即可。

注意:它基於 Konstantin Kovshenin 的想法/建議。


邊注:

如果要在預設情況下跳過/停用預設影像大小,只需在管理設定中將 0 新增為 widthheight

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。