get_cat_ID 基于名称获取类别的ID。“类别”是一种特定的分类法,名为category. 您正在尝试获取产品类别的ID(product_cat). 您不能使用get_cat_ID() 获取自定义分类的ID。
要做到这一点,您需要使用get_term_by():
$product_cat = get_term_by( \'name\', $cat_name, \'product_cat\' );
if ( $product_cat ) {
$thisCatID = $product_cat->term_id;
}
但是,根据您的代码,您不需要查找ID。您已经有了ID:
$cate_object = get_the_terms( $post->ID, \'product_cat\' );
foreach( $cate_object as $cate ){
$thisCatID = $cate->term_id; // This is the ID.
$cat_array[] = $thisCatID;
}