如何根据产品所属的产品类别的图像以编程方式设置产品的特色图像
How to programmatically set the featured image of the product based on the image of the product category to which it belongs
我有很多产品,但图片实际上是类别图片。我希望产品特色图片自动成为它们所在的类别图片。
下面的代码有问题。
function default_category_featured_image() {
global $post;
$featured_image_exists = has_post_thumbnail($post->ID);
if (!$featured_image_exists) {
$attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
if ($attached_image) {
foreach ($attached_image as $attachment_id => $attachment) {
set_post_thumbnail($post->ID, $attachment);
}}
else if ( in_category('61') ) {
set_post_thumbnail($post->ID, '4436'); //arçelik
}
else if ( in_category('64') ) {
set_post_thumbnail($post->ID, '4435'); //beko
}
else if ( in_category('59') ) {
set_post_thumbnail($post->ID, '4434'); //LG
}
else if ( in_category('60') ) {
set_post_thumbnail($post->ID, '4458'); //Philips
}
else if ( in_category('66') ) {
set_post_thumbnail($post->ID, '4160'); //Profilo
}
else if ( in_category('58') ) {
set_post_thumbnail($post->ID, '4159'); //samsung
}
else if ( in_category('65') ) {
set_post_thumbnail($post->ID, '4160'); //seg
}
else if ( in_category('62') ) {
set_post_thumbnail($post->ID, '4433'); //sony
}
else if ( in_category('63') ) {
set_post_thumbnail($post->ID, '4160'); //vestel
}
else {
set_post_thumbnail($post->ID, '4435'); //genel
wp_reset_postdata();
}
}
}
add_action('the_post', 'default_category_featured_image');
您可以使用 init 挂钩设置产品缩略图。
Make sure you remove the programmatically_set_thumbnail_to_product
function from the functions.php file once it's done.
要替换的数据:
- 输入 产品类别 ID“通用”代替
$thumbnail_ids
数组的 genel
键。
将其添加到您的活动主题 functions.php。
// updates the thumbnails of all products based on the product category image
add_action( 'init', 'programmatically_set_thumbnail_to_product' );
function programmatically_set_thumbnail_to_product() {
// returns the ids of all products
$ids = wc_get_products(
array(
'return' => 'ids',
'limit' => -1
)
);
// set the ids of the product categories
$thumbnail_ids = array(
58 => 4159, // Samsung
59 => 4434, // LG
60 => 4458, // Philips
61 => 4436, // Arçelik
62 => 4433, // Sony
63 => 4160, // Vestel
64 => 4435, // Beko
65 => 4160, // Seg
66 => 4160, // Profilo
'genel' => 4435 // Genel (set the id of the product category "Genel" as a key)
);
// for each product
foreach ( $ids as $product_id ) {
// for each product category
foreach ( $thumbnail_ids as $cat_id => $thumbnail_id ) {
// check if the product belongs to a product category
if ( has_term( $cat_id, 'product_cat', $product_id ) ) {
// if yes, set the product thumbnail
set_post_thumbnail( $product_id, $thumbnail_id);
break;
}
}
}
}
此功能将根据相应产品类别的缩略图设置所有产品的缩略图。
添加到 functions.php
后,刷新前端的产品目录页面(例如),您会注意到所有图像都将 changed/set。
Then remove it from functions.php and add this other function.
每次保存或更新产品时都会执行以下功能(自动设置新产品的图像)。
要替换的数据:
- 输入 产品类别 ID“通用”代替
$thumbnail_ids
数组的 genel
键。
始终将其添加到 functions.php。
// when a product is saved or updated it sets the image based on the product category
add_action( 'save_post', 'programmatically_set_thumbnail_to_new_product' );
function programmatically_set_thumbnail_to_new_product( $product_id ) {
// set the ids of the product categories
$thumbnail_ids = array(
58 => 4159, // Samsung
59 => 4434, // LG
60 => 4458, // Philips
61 => 4436, // Arçelik
62 => 4433, // Sony
63 => 4160, // Vestel
64 => 4435, // Beko
65 => 4160, // Seg
66 => 4160, // Profilo
'genel' => 4435 // Genel (set the id of the product category "Genel" as a key)
);
// for each product category
foreach ( $thumbnail_ids as $cat_id => $thumbnail_id ) {
// check if the product belongs to a product category
if ( has_term( $cat_id, 'product_cat', $product_id ) ) {
// retrieve product thumbnail id
$current_thumbnail_id = get_post_thumbnail_id( $product_id );
// if it is different from that of the product category
if ( $current_thumbnail_id == false || $current_thumbnail_id != $thumbnail_id ) {
// set the product thumbnail
set_post_thumbnail( $product_id, $thumbnail_id );
break;
}
}
}
}
两个代码都已经过测试并且有效。
我有很多产品,但图片实际上是类别图片。我希望产品特色图片自动成为它们所在的类别图片。
下面的代码有问题。
function default_category_featured_image() {
global $post;
$featured_image_exists = has_post_thumbnail($post->ID);
if (!$featured_image_exists) {
$attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
if ($attached_image) {
foreach ($attached_image as $attachment_id => $attachment) {
set_post_thumbnail($post->ID, $attachment);
}}
else if ( in_category('61') ) {
set_post_thumbnail($post->ID, '4436'); //arçelik
}
else if ( in_category('64') ) {
set_post_thumbnail($post->ID, '4435'); //beko
}
else if ( in_category('59') ) {
set_post_thumbnail($post->ID, '4434'); //LG
}
else if ( in_category('60') ) {
set_post_thumbnail($post->ID, '4458'); //Philips
}
else if ( in_category('66') ) {
set_post_thumbnail($post->ID, '4160'); //Profilo
}
else if ( in_category('58') ) {
set_post_thumbnail($post->ID, '4159'); //samsung
}
else if ( in_category('65') ) {
set_post_thumbnail($post->ID, '4160'); //seg
}
else if ( in_category('62') ) {
set_post_thumbnail($post->ID, '4433'); //sony
}
else if ( in_category('63') ) {
set_post_thumbnail($post->ID, '4160'); //vestel
}
else {
set_post_thumbnail($post->ID, '4435'); //genel
wp_reset_postdata();
}
}
}
add_action('the_post', 'default_category_featured_image');
您可以使用 init 挂钩设置产品缩略图。
Make sure you remove the
programmatically_set_thumbnail_to_product
function from the functions.php file once it's done.
要替换的数据:
- 输入 产品类别 ID“通用”代替
$thumbnail_ids
数组的genel
键。
将其添加到您的活动主题 functions.php。
// updates the thumbnails of all products based on the product category image
add_action( 'init', 'programmatically_set_thumbnail_to_product' );
function programmatically_set_thumbnail_to_product() {
// returns the ids of all products
$ids = wc_get_products(
array(
'return' => 'ids',
'limit' => -1
)
);
// set the ids of the product categories
$thumbnail_ids = array(
58 => 4159, // Samsung
59 => 4434, // LG
60 => 4458, // Philips
61 => 4436, // Arçelik
62 => 4433, // Sony
63 => 4160, // Vestel
64 => 4435, // Beko
65 => 4160, // Seg
66 => 4160, // Profilo
'genel' => 4435 // Genel (set the id of the product category "Genel" as a key)
);
// for each product
foreach ( $ids as $product_id ) {
// for each product category
foreach ( $thumbnail_ids as $cat_id => $thumbnail_id ) {
// check if the product belongs to a product category
if ( has_term( $cat_id, 'product_cat', $product_id ) ) {
// if yes, set the product thumbnail
set_post_thumbnail( $product_id, $thumbnail_id);
break;
}
}
}
}
此功能将根据相应产品类别的缩略图设置所有产品的缩略图。
添加到 functions.php
后,刷新前端的产品目录页面(例如),您会注意到所有图像都将 changed/set。
Then remove it from functions.php and add this other function.
每次保存或更新产品时都会执行以下功能(自动设置新产品的图像)。
要替换的数据:
- 输入 产品类别 ID“通用”代替
$thumbnail_ids
数组的genel
键。
始终将其添加到 functions.php。
// when a product is saved or updated it sets the image based on the product category
add_action( 'save_post', 'programmatically_set_thumbnail_to_new_product' );
function programmatically_set_thumbnail_to_new_product( $product_id ) {
// set the ids of the product categories
$thumbnail_ids = array(
58 => 4159, // Samsung
59 => 4434, // LG
60 => 4458, // Philips
61 => 4436, // Arçelik
62 => 4433, // Sony
63 => 4160, // Vestel
64 => 4435, // Beko
65 => 4160, // Seg
66 => 4160, // Profilo
'genel' => 4435 // Genel (set the id of the product category "Genel" as a key)
);
// for each product category
foreach ( $thumbnail_ids as $cat_id => $thumbnail_id ) {
// check if the product belongs to a product category
if ( has_term( $cat_id, 'product_cat', $product_id ) ) {
// retrieve product thumbnail id
$current_thumbnail_id = get_post_thumbnail_id( $product_id );
// if it is different from that of the product category
if ( $current_thumbnail_id == false || $current_thumbnail_id != $thumbnail_id ) {
// set the product thumbnail
set_post_thumbnail( $product_id, $thumbnail_id );
break;
}
}
}
}
两个代码都已经过测试并且有效。