2013-03-18 107 views
1

目前,我的特色圖像正在產品循環中使用(如默認)。WooCommerce - 如何阻止小產品圖像使產品環路中的較大產品圖像變矮?

問題在於,因爲所有產品圖像的尺寸都調整爲固定尺寸(對於更好的佈局),較小產品的圖像實際上使較大產品的圖像變矮。

除非任何人有更好的建議,否則我決定解決這個問題的一種方法是在產品環路中使用不同的產品圖像。這樣,我可以在較小的產品圖像上添加一個空白區域,以便在調整大小時不會使其他產品變得不合適。 (注意 - 我不能將該編輯後的圖像設置爲特色圖像,因爲我不希望將此空白區域顯示在燈箱中。)

因此,首先,解決我的問題的最佳方法是?而且,如果是這樣,那麼指定要在產品循環中使用的自定義圖像的最佳方法是什麼?

在此先感謝。

回答

2

這是我想出來解決這個問題的功能。它需要放在你的functions.php文件中。請參閱功能使用說明...

/** 
* This function is used to display a custom image in the shop page if a custom 
* image exists for the product. This may be required, for example, to prevent 
* images of small products dwarfing images of larger products on the shop page. 
* 
* To set a custom image for a product, create a custom field for it (in the 
* Edit Product page) called custom_product_thumbnail_url . (The image that you 
* specify can contain additional white space to prevent it being enlarged.) 
* 
* This function overrides the function of the same name in WooCommerce's 
* woocommerce-template.php file. 
* 
* @access public 
* @subpackage Loop 
* @param string $size (default: 'shop_catalog') 
* @param int $placeholder_width (default: 0) 
* @param int $placeholder_height (default: 0) 
* @return string 
*/ 
function woocommerce_get_product_thumbnail($size = 'shop_catalog', $placeholder_width = 0, $placeholder_height = 0 ) { 

    global $post, $woocommerce; 

    if (! $placeholder_width) 
      $placeholder_width = $woocommerce->get_image_size('shop_catalog_image_width'); 
    if (! $placeholder_height) 
      $placeholder_height = $woocommerce->get_image_size('shop_catalog_image_height'); 

    $imgSrc = get_post_meta($post->ID, 'custom_product_thumbnail_url', true); 
    if ($imgSrc) 
      return '<img src="'. $imgSrc .'" alt="' . get_the_title($post->ID) . '" width="' . $placeholder_width . '" height="' . $placeholder_height . '" />'; 
    if (has_post_thumbnail()) 
      return get_the_post_thumbnail($post->ID, $size); 
    elseif (woocommerce_placeholder_img_src()) 
      return '<img src="'. woocommerce_placeholder_img_src() .'" alt="Placeholder" width="' . $placeholder_width . '" height="' . $placeholder_height . '" />'; 

}