如何更改WooCommerce缩略图裁剪位置


How to change WooCommerce thumbnail crop position?

我正在尝试更改WooCommerce缩略图裁剪位置。我发现这段代码可以帮助更改大小:

add_action( 'init', 'yourtheme_woocommerce_image_dimensions', 1 );
/**
 * Define image sizes
 */
function yourtheme_woocommerce_image_dimensions() {
    $catalog = array(
        'width'     => '100',   // px
        'height'    => '100',   // px
        'crop'      => 0
    );
    // Image sizes
    update_option( 'shop_catalog_image_size', $catalog );       // Product category thumbs
}

我做了一些尝试,例如将裁剪0更改为array("center", "bottom"),但它不起作用:

function yourtheme_woocommerce_image_dimensions() {
    $catalog = array(
    'width'   => '300', // px
    'height'  => '400', // px
    'crop'    => 'array("center", "bottom")'
  );
  // Image sizes
  update_option( 'shop_catalog_image_size', $catalog );     // Product category thumbs
}

而且这没有成功:

if (function_exists( 'add_image_size' )){
  add_image_size( 'shop_catalog', 300, 400, array( 'center', 'bottom' ) );
}

无论如何我可以改变它吗?

谢谢。

要更改活动子主题或主题中的现有图像大小(裁剪选项),您需要使用 WordPress 钩子'after_switch_theme'

由于WordPress 3.9 +是众多新功能中的一个很棒的功能,因此增加了控制WordPress中上传的图像裁剪位置的功能。
我不知道裁剪药水高级选项是否可用于 woocommerce 图像大小,您必须对其进行测试。

作物位置的可用选项包括:

left top
left center
left bottom
right top
right center
right bottom
center top
center center
center bottom

因此,根据wooThemes的这个片段和WordPress的(这个相对较新的)裁剪选项,你可以试试这个

function yourtheme_woocommerce_image_dimensions() {
    global $pagenow;
    if ( ! isset( $_GET['activated'] ) || $pagenow != 'themes.php' ) {
        return;
    }
    $catalog = array(
        'width'     => '300',   // px
        'height'    => '400',   // px
        'crop'      => array( 'center', 'bottom' ) // New crop options to try.
    );
    /* $single = array(
        'width'     => '600',   // px
        'height'    => '600',   // px
        'crop'      => 1        // true
    );
    $thumbnail = array(
        'width'     => '120',   // px
        'height'    => '120',   // px
        'crop'      => 0        // false
    ); */
    // 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', 'yourtheme_woocommerce_image_dimensions', 1 );

您需要将此代码片段粘贴到活动子主题或主题的函数.php文件中...

您可以注释/取消注释代码(或删除某些部分)以满足您的需求。此代码将覆盖 WooCommerce 设置中的定义选项>产品>显示(产品图像)。

激活:
您需要将活动主题切换到另一个主题,然后切换回来以激活它。

引用:

  • 在主题激活时设置WooCommerce图像尺寸