如何更改woocomerce中加载的图像大小?

时间:2019-08-04 12:00:13

标签: php wordpress woocommerce hook-woocommerce

在我的网站上,我有一个滑块可以加载产品图片的620 * 620尺寸。我想加载此图片的其他尺寸(我想要的尺寸是在wordpress画廊中创建的,但我不知道如何加载它们)。 默认图片大小在“ woocomerce_thumbnail”中。 我可以使用的其他尺寸是多少?

我在这里找到了一些东西,但是如何更改此默认大小? (我还能使用什么其他尺寸?)

if (!function_exists( 'woocommerce_get_product_thumbnail' ) ) {

    /**
     * Get the product thumbnail, or the placeholder if not set.
     *
     * @param string $size (default: 'woocommerce_thumbnail').
     * @param int    $deprecated1 Deprecated since WooCommerce 2.0 (default: 0).
     * @param int    $deprecated2 Deprecated since WooCommerce 2.0 (default: 0).
     * @return string
     */
    function woocommerce_get_product_thumbnail( $size = 'woocommerce_thumbnail', $deprecated1 = 0, $deprecated2 = 0 ) {
        global $product;

        $image_size = apply_filters( 'single_product_archive_thumbnail_size', $size );

        return $product ? $product->get_image( $image_size ) : '';
    }
}

2 个答案:

答案 0 :(得分:0)

  

这些大小包括:

     
      
  • woocommerce_thumbnail –在商店页面等地方的产品“网格”中使用。
  •   
  • woocommerce_single –在单个产品页面上使用。 woocommerce_gallery_thumbnail –在
    的主图像下方使用   单个产品页面即可切换图库。
  •   
  • woocommerce_single显示完整的产品图片(已上传),因此默认情况下始终未裁剪。默认宽度为600px。
  •   
     

woocommerce_gallery_thumbnail始终为正方形裁剪,默认为   100×100像素。这用于浏览图库中的图像。

     

woocommerce_thumbnail的默认宽度为600像素(正方形裁剪),因此   产品网格看起来很整洁。裁剪的宽高比可以为   由店主定制。

您可以找到上述内容以及有关定义自定义尺寸here的信息。

答案 1 :(得分:0)

重新检查您的主题支持add_theme_support('post-thumbnails');

仅适用于woocommerce,您可以尝试

if (!function_exists('firefog_woocommerce_image_dimensions')) {
    function firefog_woocommerce_image_dimensions() {
        global $pagenow;

        if ( ! isset( $_GET['activated'] ) || $pagenow != 'themes.php' ) {
            return;
        }

        $catalog = array(
            'width'     => '300',   // px
            'height'    => '300',   // px
            'crop'      => 1        // true
        );

        $single = array(
            'width'     => '500',   // px
            'height'    => '500',   // px
            'crop'      => 1        // true
        );

        $thumbnail = array(
            'width'     => '100',   // px
            'height'    => '100',   // px
            'crop'      => 1        // true
        );

        // Image sizes
        update_option( 'shop_catalog_image_size', $catalog );       // Product category thumbs
        update_option( 'shop_single_image_size', $single );         // Single product image
        update_option( 'shop_thumbnail_image_size', $thumbnail );   // Image gallery thumbs
    }
}
add_action( 'after_switch_theme', 'firefog_woocommerce_image_dimensions', 1 );

用于默认WordPress缩略图

the_post_thumbnail( 'thumbnail' );     // Thumbnail (150 x 150 hard cropped)
the_post_thumbnail( 'medium' );        // Medium resolution (300 x 300 max height 300px)
the_post_thumbnail( 'medium_large' );  // Medium Large (added in WP 4.4) resolution (768 x 0 infinite height)
the_post_thumbnail( 'large' );         // Large resolution (1024 x 1024 max height 1024px)
the_post_thumbnail( 'full' );          // Full resolution (original size uploaded)

如果您需要特定的分辨率

the_post_thumbnail( array(500, 500) );  // 500x500 dimension 

发布缩略图链接到大尺寸图片,您可以通过更改large

来更改尺寸
if ( has_post_thumbnail() ) {
    $large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large' );
    if ( ! empty( $large_image_url[0] ) ) {
        printf( '<a href="%1$s" alt="%2$s">%3$s</a>',
            esc_url( $large_image_url[0] ),
            esc_attr( get_the_title_attribute( 'echo=0' ) ),
            get_the_post_thumbnail()
        );
    }
}

您还可以在主题的功能中创建自定义的特色图片尺寸

 add_image_size( 'custom-thumb', 300, 300); //Simple widget size
 add_image_size( 'sidebar-thumb', 120, 120, true ); // Hard Crop Mode
 add_image_size( 'homepage-thumb', 220, 180 ); // Soft Crop Mode
 add_image_size( 'singlepost-thumb', 590, 9999 ); // Unlimited Height Mode

以下是如何在主题的functions.php文件中创建自定义“特色图片”尺寸的示例。

if ( function_exists( 'add_theme_support' ) ) {
    add_theme_support( 'post-thumbnails' );
    add_image_size( 'custom-thumb', 300, 300); // 300 pixels wide 
 }

在WordPress主题中显示其他图像大小

the_post_thumbnail( 'your-specified-image-size' ); //Example singlepost-thumb