【问题标题】:WooCommerce - how to stop small product images dwarfing larger product images in the product loop?WooCommerce - 如何在产品循环中阻止小产品图片使大产品图片相形见绌?
【发布时间】:2013-03-07 02:22:24
【问题描述】:

目前,我的特色图片正在产品循环中使用(默认情况下)。

问题在于,由于所有产品图片都被调整为固定尺寸(为了更好的布局),因此较小产品的图片实际上使较大产品的图片相形见绌。

除非有人有更好的建议,否则我决定解决此问题的一种方法是在产品循环中使用不同的产品图片。这样,我可以在较小的产品图像上方添加一个空白区域,这样它们在调整大小时就不会使其他产品相形见绌。 (注意 - 我不能只将此编辑后的图像设置为特色图像,因为我不希望此空白区域显示在灯箱中。)

那么,首先,这是解决我的问题的最佳方法吗?如果是这样,指定要在产品循环中使用的自定义图像的最佳方法是什么?

提前致谢。

【问题讨论】:

    标签: image loops overriding product woocommerce


    【解决方案1】:

    这是我想出的解决这个问题的功能。它需要放在您的functions.php 文件中。用法见函数 cmets...

    /**
     * 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 . '" />';
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-10
      • 2022-06-22
      • 1970-01-01
      • 2020-06-22
      • 2013-07-05
      • 2016-09-10
      • 2016-03-14
      • 1970-01-01
      相关资源
      最近更新 更多