我有很多产品,但图片实际上是类别图片。我希望产品特色图片自动成为它们所在的类别图片。
下面的代码有问题。
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');
答案 0 :(得分:1)
您可以使用 init 钩子设置产品缩略图。
<块引用>确保programmatically_set_thumbnail_to_product
函数完成后从functions.php 文件中删除。
要替换的数据:
genel
数组的 $thumbnail_ids
键。将其添加到您的活动主题的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
后(例如)刷新前端中的产品目录页面,您会注意到所有图像都将被更改/设置。
然后从functions.php中删除它并添加另一个函数。
每次保存或更新产品时都会执行以下功能(自动设置新产品的图像)。
要替换的数据:
genel
数组的 $thumbnail_ids
键。始终将其添加到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;
}
}
}
}
两个代码都已经过测试并且可以工作。