2014-02-27 3 views
0

안녕하십니까, 단일 제품 카테고리에 따라 헤더를 변경하려고합니다. 나는 상위 범주 인 경우와 <?php get_header('lawn'); ?>을 할 헤더를 필요로하는-잔디 매장의 상위 범주에 해당 항목을 볼 때 나는 내 제품 카테고리는 기본적으로이WooCommerce 사용자 정의 단일 제품 템플릿

- the-lawn-store 
- - turf 
- - grass-seed 
- - wildflower-turf 

- the-oak-store 
- - railway-sleepers 
- - pergolas 

처럼 워드 프레스 & WooCommerce 을 사용하고 있습니다 the-oak-store 나는 헤더가 <?php get_header('oak'); ?> 인 헤더가 필요하다. 헤더의 차이는 모든 페이지의 스타일링이다! 이것에 대해 가장 좋은 방법은 무엇입니까?

답변

1

글쎄, 필요한 것은 상위 카테고리입니다. 그렇게하기 위해서는, 우선이와 부모 ID를 얻을 수 있습니다 :

global $wp_query; 

$cat_obj = $wp_query->get_queried_object(); 

if($cat_obj) { 
    //print_r($cat_obj); 
    $category_ID = $cat_obj->term_id; 
    $category_parent = $cat_obj->parent; 
    $category_taxonomy = $cat_obj->taxonomy; 

    $category_parent_term = get_term_by('id', absint($category_ID), $category_taxonomy); 
    $category_parent_slug = $category_parent_term->slug; 

    get_header($category_parent_slug); 

}else{ 

    get_header(); 

    } 

주석 인 print_r를 사용할 수 바르의 나머지 부분을 볼 수 있습니다. 내 로컬 우와에서 작동 테스트.

1

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'); 
관련 문제