WooCommerce - 如何阻止小产品图像使产品环路中的较大产品图像变矮?
问题描述:
目前,我的特色图像正在产品循环中使用(如默认)。WooCommerce - 如何阻止小产品图像使产品环路中的较大产品图像变矮?
问题在于,因为所有产品图像的尺寸都调整为固定尺寸(对于更好的布局),较小产品的图像实际上使较大产品的图像变矮。
除非任何人有更好的建议,否则我决定解决这个问题的一种方法是在产品环路中使用不同的产品图像。这样,我可以在较小的产品图像上添加一个空白区域,以便在调整大小时不会使其他产品变得不合适。 (注意 - 我不能将该编辑后的图像设置为特色图像,因为我不希望将此空白区域显示在灯箱中。)
因此,首先,解决我的问题的最佳方法是?而且,如果是这样,那么指定要在产品循环中使用的自定义图像的最佳方法是什么?
在此先感谢。
答
这是我想出来解决这个问题的功能。它需要放在你的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 . '" />';
}