为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
慕妹3242003 2021-11-13 16:01:54
在我的网站中,我有一个滑块可以加载 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 回答

?
慕工程0101907

TA贡献1887条经验 获得超5个赞

重新检查您的主题支持 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 

Post Thumbnail Linking to Large Image Size您可以通过更改来更改大小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


查看完整回答
反对 回复 2021-11-13
?
萧十郎

TA贡献1815条经验 获得超13个赞

这些尺寸包括:

  • woocommerce_thumbnail – 用于产品“网格”中的商店页面等地方。

  • woocommerce_single– 用于单个产品页面。woocommerce_gallery_thumbnail – 用于
    单品页面主图下方切换图库。

  • woocommerce_single显示上传的完整产品图像,因此默认情况下始终未裁剪。它默认为 600px 宽度。

woocommerce_gallery_thumbnail总是方形裁剪,默认为 100×100 像素。这用于在图库中导航图像。

woocommerce_thumbnail默认为 600px 宽度,方形裁剪,使产品网格看起来整洁。裁剪的纵横比可由店主自定义。

您可以在此处找到有关定义自定义尺寸的上述内容和信息。


查看完整回答
反对 回复 2021-11-13
  • 2 回答
  • 0 关注
  • 185 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号