gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 20:34:58 25 4
gpt4 key购买 nike

我正在尝试更改 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
}

我做了一些尝试,比如将 crop 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' ) );
}

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

谢谢。

最佳答案

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

自 WordPress 3.9+ 以来,一个很棒的新功能是现在可以控制在 WordPress 中上传的图像的裁剪位置。
我不知道 crop potion 高级选项是否适用于 woocommerce 图像大小,你将不得不测试它。

裁剪位置的可用选项是:

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

所以基于this snippet从 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 );

您需要将此代码片段粘贴到您的事件子主题或主题的 function.php 文件中......

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

ACTIVATION:
You will need to switch your active theme to another and then switch back to activate it.

引用资料:

关于php - 如何更改 WooCommerce 缩略图裁剪位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38236848/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com