如何更改WooCommerce缩略图的裁剪位置? [英] How to change WooCommerce thumbnail crop position?
问题描述
我正在尝试更改WooCommerce缩略图的裁剪位置.我发现此代码可以帮助更改大小:
I'm trying to change WooCommerce thumbnail crop position. I found this code can help to change the size:
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")
,但是它不起作用:
I did some try like change crop 0
to array("center", "bottom")
but it's not working:
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' ) );
}
反正我可以更改它吗?
Is there anyway I can change it?
谢谢.
推荐答案
要在活动的子主题或主题中更改现有图像的大小(裁剪选项),您需要使用'after_switch_theme'
WordPress挂钩.
To change existing images sizes (crop option) within your active child theme or theme, you need to use 'after_switch_theme'
WordPress hook.
自WordPress 3.9+以来,许多功能中的一项了不起的新功能是增加了控制WordPress中上载图像的裁剪位置的功能. 我不知道woocommerce图片尺寸是否可以使用作物药水"高级选项,您将不得不对其进行测试.
Since WordPress 3.9+ an awesome new feature among many, is the added ability to now control crop position of images uploaded in WordPress.
I don't know if crop potion advanced option is available to woocommerce images sizes, you will have to test it.
作物位置的可用选项为:
The available options for crop position are:
left top
left center
left bottom
right top
right center
right bottom
center top
center center
center bottom
因此基于 此代码段 (来自wooThemes和WordPress的(相对较新的)裁剪选项),您可以尝试以下操作:
So based on this snippet from wooThemes and and (this relative new) crop options of WordPress, you can try this:
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设置>产品>显示(产品图片)中的定义选项.
You can comment/uncomment the code (or remove some portions) to feet your needs. This code will overwrite define options in WooCommerce settings > Products > Display (Product Images).
激活:
您需要将活动主题切换,然后再切换回激活主题.
ACTIVATION:
You will need to switch your active theme to another and then switch back to activate it.
参考文献:
这篇关于如何更改WooCommerce缩略图的裁剪位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!