我想出来了,万一有人需要,
它非常简单,只是在找不到id时使用了elseif&;将该分类id的自定义字段名称更改为自定义分类图像字段(workplace\\u thumb)。
if ( ! function_exists(\'custom_subcategory_thumbnail\') ):
remove_action(\'woocommerce_before_subcategory_title\', \'woocommerce_subcategory_thumbnail\');
add_action(\'woocommerce_before_subcategory_title\', \'custom_subcategory_thumbnail\', 10, 1);
function custom_subcategory_thumbnail( $category ) {
$thumbnail_id = get_term_meta( $category->term_id, \'thumbnail_id\', true );
/* $term = get_field(\'workplace_thumb\', \'workplace_\' . $category->post_ID);
echo \'<pre>\';
echo "thumbnail id variable value vardump: ";echo \'<br />\';
var_dump($thumbnail_id); echo \'<br />\';
echo \'term variable value: \';echo \'<br />\';
var_dump($term);echo \'<br />\';
echo "Category value:";echo \'<br />\';
var_dump($category);echo \'<br />\';
echo \'</pre>\';*/
// $taxonomy = $queried_object->taxonomy;
// $wpimage = get_field(\'workplace_thumb\', \'workplaces\' . \'_\' . $category->term_id);
/* echo \'<pre>\';
print_r($thumbnail_id);echo \'</pre>\';*/
// $attachment_id = get_field( \'workplace_thumb\', \'workplaces_\' . $category->term_id );
/*
echo \'<pre>\';
print_r($attachment_id);
echo \'</pre>\';*/
if ( $thumbnail_id ) {
$image = wp_get_attachment_image_src( $thumbnail_id, \'woocommerce_single\' );
$image = $image[0];
$image_srcset = function_exists( \'wp_get_attachment_image_srcset\' ) ? wp_get_attachment_image_srcset( $thumbnail_id, \'shop_single_image_size\' ) : false;
$image_sizes = function_exists( \'wp_get_attachment_image_sizes\' ) ? wp_get_attachment_image_sizes( $thumbnail_id, \'shop_single_image_size\' ) : false;
}elseif ( ! $thumbnail_id ){
$thumbnail_id_workplace = get_term_meta( $category->term_id, \'workplace_thumb\', true );
$image = wp_get_attachment_image_src( $thumbnail_id_workplace, \'woocommerce_single\' );
$image = $image[0];
$image_srcset = function_exists( \'wp_get_attachment_image_srcset\' ) ? wp_get_attachment_image_srcset( $thumbnail_id_workplace, \'shop_single_image_size\' ) : false;
$image_sizes = function_exists( \'wp_get_attachment_image_sizes\' ) ? wp_get_attachment_image_sizes( $thumbnail_id_workplace, \'shop_single_image_size\' ) : false;
}
else{
$image = wc_placeholder_img_src();
$image_srcset = $image_sizes = false;
}
if ( $image ) {
// Prevent esc_url from breaking spaces in urls for image embeds
// Ref: https://core.trac.wordpress.org/ticket/23605
$image = str_replace( \' \', \'%20\', $image );
echo \'<div class="banner-box__image brand-grayscale">
<a href="\' . esc_url( get_term_link( $category, \'product_cat\' ) ) . \'" >
<span class=" woocommerce-loop-category__thumbnail brand-grayscale" style="background-image :url(\' . esc_url( $image ) . \');"></span></a>
</div>\';
}
}
endif;