get_header()
함수를 필터링 할 수 없으므로 WooCommerce의 single-product.php
템플릿을 재정의해야합니다. 당신이 파일의 시작 부분을 수정할 수 있습니다 거기에서 :
get_header('shop'); ?>
나는 어떤 제품의 최상위 제품 카테고리 얻으려면 다음 함수를 만들어 :에서 다음
function kia_get_the_top_level_product_category($post_id = null){
$product_cat_parent = null;
if(! $post_id){
global $post;
$post_id = $post->ID;
}
// get the product's categories
$product_categories = get_the_terms($product_id, 'product_cat');
if(is_array($product_categories)) {
// gets complicated if multiple categories, so limit to one
// on the backend you can restrict to a single category with my Radio Buttons for Taxonomies plugin
$product_cat = array_shift($product_categories);
$product_cat_id = $product_cat->term_id;
while ($product_cat_id) {
$cat = get_term($product_cat_id, 'product_cat'); // get the object for the product_cat_id
$product_cat_id = $cat->parent; // assign parent ID (if exists) to $product_cat_id
// the while loop will continue whilst there is a $product_cat_id
// when there is no longer a parent $product_cat_id will be NULL so we can assign our $product_cat_parent
$product_cat_parent = $cat->slug;
}
}
return $product_cat_parent;
}
를 테마의 single-product.php
당신이 할 수있는 : 당신이 다음 특정 header-shop.php
이없는 경우
$parent = kia_get_the_top_level_product_category();
if($parent == 'oak'){
get_header('oak');
} elseif($parent == 'lawn'){
get_header('lawn');
} else {
get_header('shop');
}
당신은 기술적으로도 할 수 :
,
$parent = kia_get_the_top_level_product_category();
get_header($parent);
이 템플릿을 재정의하면 WooCommerce가 업그레이드 될 때 위험에 처할 수 있습니다. 대안으로 바디 클래스를 필터링하는 것이 좋습니다.
function wpa_22066003_body_class($c){
if(function_exists('is_product') && is_product() && $parent = kia_get_the_top_level_product_category()){
$c[] = $parent . '-product-category';
}
return $c;
}
add_filter('body_class', 'wpa_22066003_body_class');