2014-10-01 21 views
0

裏面WordPress的定義一個新的croping圖像大小,我需要生成並創建上傳文件夾內,有一個新的剪切圖像尺寸:在WordPress的

  • 寬度= 205px
  • 高度= 120像素

這裏面我function.php是我的代碼:

// Call function on after setup 
add_action('after_setup_theme', 'theme_setup_img'); 
function theme_setup_img() { 
    add_theme_support('post-thumbnails'); 
    add_image_size('search-thumb', 205, 120, true); 
    // set_post_thumbnail_size(205, 120, true); 
} 

然而,沒有新的圖像尺寸已經是創建在上傳文件夾內(只有默認的WP大小)。任何解決方案

注:我使用的是默認主題和最新的WP版本

感謝

+0

這隻適用於新上傳。如果你想調整現有圖像的大小,你需要編寫一個遍歷所有圖像並重寫圖像的函數。在[這個問題]中看到我的回答(http://stackoverflow.com/a/14643131/212107)。 – 2014-10-01 13:17:32

+0

[Big square wordpress post thumbnails]的可能重複(http://stackoverflow.com/questions/14642567/big-square-wordpress-post-thumbnails) – 2014-10-01 13:22:32

+0

即使對於新上載,也不會生成新的圖片大小。爲了讓你知道,我的圖像是2000px×1500px,所以作物應該可以工作。我試過沒有安裝任何插件,仍然是同樣的問題... 難道是PHP版本? – Romain 2014-10-01 13:30:38

回答

0

這裏的問題是「Dynamic Image Resizer」插件。 這是用Wordpress 4.0打破我的主題。

2

您的代碼看起來是正確的。礦山略有不同,我知道它的工作原理:

if (function_exists('add_theme_support')) 
{ 
    // Add Thumbnail Theme Support 
    add_theme_support('post-thumbnails'); 
    add_image_size('large', 700, '', true); // Large Thumbnail 
    add_image_size('medium', 250, '', true); // Medium Thumbnail 
} 

這是由託德座右銘從HTML5 Blank Theme拍攝。傑拉德還提到要編寫一個腳本來重新渲染,但是有一個很好的插件叫Regenerate Thumbnails,它可以做同樣的事情。

+0

即使使用您的代碼,我並未在上傳文件夾內創建新圖像 – Romain 2014-10-06 16:31:31

+0

這是一個插件問題 – Romain 2014-10-06 17:58:00

2

這可能是由於你調用「after_setup_theme」行動中的新的大小...我用下面的代碼:

// Add custom image sizes 
if(function_exists('add_image_size')) { 
    add_image_size('search-thumb', 205, 120, true); 
} 

和它的作品每一次...如果它的功能中。 PHP文件,你不需要一個動作或鉤子來使它工作。

此外,您還可以添加此添加到functions.php,使您的自定義尺寸在下拉菜單中顯示出來的圖像插入到頁/職位/何地時:

// Functions to add custom image sizes to the media library thickbox area 
// and put them into drop down 
function my_insert_custom_image_sizes($sizes) { 
    // get the custom image sizes 
    global $_wp_additional_image_sizes; 
    // if there are none, just return the built-in sizes 
    if (empty($_wp_additional_image_sizes)) 
     return $sizes; 
    // add all the custom sizes to the built-in sizes 
    foreach ($_wp_additional_image_sizes as $id => $data) { 
     // take the size ID (e.g., 'my-name'), replace hyphens with spaces, 
     // and capitalise the first letter of each word 
     if (!isset($sizes[$id])) 
      $sizes[$id] = ucfirst(str_replace('-', ' ', $id)); 
    } 
    return $sizes; 
} 
add_filter('image_size_names_choose', 'my_insert_custom_image_sizes'); 

您將需要一個插件(我使用AJAX Thumbnail Rebuild)來調整在執行此代碼之前已上傳的舊圖像的大小。

+0

感謝您的回覆。爲了讓你知道,鉤子在function.php之外被使用。 – Romain 2014-10-06 17:48:12