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

在我的网站中,我有一个滑块可以加载 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 ) : '';

    }

}


慕妹3242003
浏览 187回答 2
2回答

慕工程0101907

重新检查您的主题支持 add_theme_support('post-thumbnails');只有 woocommerce 你可以试试这个if (!function_exists('firefog_woocommerce_image_dimensions')) {&nbsp; &nbsp; function firefog_woocommerce_image_dimensions() {&nbsp; &nbsp; &nbsp; &nbsp; global $pagenow;&nbsp; &nbsp; &nbsp; &nbsp; if ( ! isset( $_GET['activated'] ) || $pagenow != 'themes.php' ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $catalog = array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'width'&nbsp; &nbsp; &nbsp;=> '300',&nbsp; &nbsp;// px&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'height'&nbsp; &nbsp; => '300',&nbsp; &nbsp;// px&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'crop'&nbsp; &nbsp; &nbsp; => 1&nbsp; &nbsp; &nbsp; &nbsp; // true&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; $single = array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'width'&nbsp; &nbsp; &nbsp;=> '500',&nbsp; &nbsp;// px&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'height'&nbsp; &nbsp; => '500',&nbsp; &nbsp;// px&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'crop'&nbsp; &nbsp; &nbsp; => 1&nbsp; &nbsp; &nbsp; &nbsp; // true&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; $thumbnail = array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'width'&nbsp; &nbsp; &nbsp;=> '100',&nbsp; &nbsp;// px&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'height'&nbsp; &nbsp; => '100',&nbsp; &nbsp;// px&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'crop'&nbsp; &nbsp; &nbsp; => 1&nbsp; &nbsp; &nbsp; &nbsp; // true&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; // Image sizes&nbsp; &nbsp; &nbsp; &nbsp; update_option( 'shop_catalog_image_size', $catalog );&nbsp; &nbsp; &nbsp; &nbsp;// Product category thumbs&nbsp; &nbsp; &nbsp; &nbsp; update_option( 'shop_single_image_size', $single );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Single product image&nbsp; &nbsp; &nbsp; &nbsp; update_option( 'shop_thumbnail_image_size', $thumbnail );&nbsp; &nbsp;// Image gallery thumbs&nbsp; &nbsp; }}add_action( 'after_switch_theme', 'firefog_woocommerce_image_dimensions', 1 );对于默认 WordPress 缩略图the_post_thumbnail( 'thumbnail' );&nbsp; &nbsp; &nbsp;// Thumbnail (150 x 150 hard cropped)the_post_thumbnail( 'medium' );&nbsp; &nbsp; &nbsp; &nbsp; // Medium resolution (300 x 300 max height 300px)the_post_thumbnail( 'medium_large' );&nbsp; // Medium Large (added in WP 4.4) resolution (768 x 0 infinite height)the_post_thumbnail( 'large' );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Large resolution (1024 x 1024 max height 1024px)the_post_thumbnail( 'full' );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Full resolution (original size uploaded)如果您需要特定的分辨率the_post_thumbnail( array(500, 500) );&nbsp; // 500x500 dimension&nbsp;Post Thumbnail Linking to Large Image Size您可以通过更改来更改大小largeif ( has_post_thumbnail() ) {&nbsp; &nbsp; $large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large' );&nbsp; &nbsp; if ( ! empty( $large_image_url[0] ) ) {&nbsp; &nbsp; &nbsp; &nbsp; printf( '<a href="%1$s" alt="%2$s">%3$s</a>',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; esc_url( $large_image_url[0] ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; esc_attr( get_the_title_attribute( 'echo=0' ) ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; get_the_post_thumbnail()&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; }}您还可以在主题功能中创建自定义特色图像大小&nbsp;add_image_size( 'custom-thumb', 300, 300); //Simple widget size&nbsp;add_image_size( 'sidebar-thumb', 120, 120, true ); // Hard Crop Mode&nbsp;add_image_size( 'homepage-thumb', 220, 180 ); // Soft Crop Mode&nbsp;add_image_size( 'singlepost-thumb', 590, 9999 ); // Unlimited Height Mode以下是如何在主题functions.php文件中创建自定义特色图像大小的示例。if ( function_exists( 'add_theme_support' ) ) {&nbsp; &nbsp; add_theme_support( 'post-thumbnails' );&nbsp; &nbsp; add_image_size( 'custom-thumb', 300, 300); // 300 pixels wide&nbsp;&nbsp;}在您的 WordPress 主题中显示其他图像大小the_post_thumbnail( 'your-specified-image-size' ); //Example singlepost-thumb

萧十郎

这些尺寸包括:woocommerce_thumbnail&nbsp;– 用于产品“网格”中的商店页面等地方。woocommerce_single– 用于单个产品页面。woocommerce_gallery_thumbnail – 用于单品页面主图下方切换图库。woocommerce_single显示上传的完整产品图像,因此默认情况下始终未裁剪。它默认为 600px 宽度。woocommerce_gallery_thumbnail总是方形裁剪,默认为 100×100 像素。这用于在图库中导航图像。woocommerce_thumbnail默认为 600px 宽度,方形裁剪,使产品网格看起来整洁。裁剪的纵横比可由店主自定义。您可以在此处找到有关定义自定义尺寸的上述内容和信息。
打开App,查看更多内容
随时随地看视频慕课网APP