2014-10-02 27 views
6

Tôi đang cố gắng thêm danh mục gốc của sản phẩm từ Woocommerce làm lớp học vào thẻ 'body' của wordpress.Thêm danh mục mẹ của Woocommerce vào lớp WP 'body'

Mỗi lần tôi đi trong danh mục con, danh mục gốc không còn nằm trong lớp body.

Có thể một số nội dung như dưới đây được chỉnh sửa để tìm danh mục gốc và thêm trong thẻ body?

Có thể một cụm từ như "product_parent_cat"? Cố gắng này và đã tìm kiếm API của họ, nhưng không thành công ..

function woo_custom_taxonomy_in_body_class($classes){ 
    $custom_terms = get_the_terms(0, 'product_cat'); 
    if ($custom_terms) { 
     foreach ($custom_terms as $custom_term) { 
     $classes[] = 'product_cat_' . $custom_term->slug; 
     } 
    } 
    return $classes; 
} 

add_filter('body_class', 'woo_custom_taxonomy_in_body_class'); 

Trả lời

7

Bạn có thể thử thay đổi này (chưa được kiểm tra):

function woo_custom_taxonomy_in_body_class($classes){ 
    $custom_terms = get_the_terms(0, 'product_cat'); 
    if ($custom_terms) { 
     foreach ($custom_terms as $custom_term) { 

     // Check if the parent category exists: 
     if($custom_term->parent > 0) { 
      // Get the parent product category: 
      $parent = get_term($custom_term->parent, 'product_cat'); 
      // Append the parent class: 
      if (! is_wp_error($parent)) 
       $classes[] = 'product_parent_cat_' . $parent->slug; 
     } 

     $classes[] = 'product_cat_' . $custom_term->slug; 
     } 
    } 
    return $classes; 
} 

add_filter('body_class', 'woo_custom_taxonomy_in_body_class'); 

để thêm sên loại sản phẩm mẹ đến lớp cơ thể.

Ở đây chúng tôi sử dụng thuộc tính parent của đối tượng cụm từ được trả về bởi hàm get_term().

+0

cảm ơn bạn! đóng đinh nó. – blkedy

+0

@ user1420650 Vui mừng khi biết điều đó phù hợp với bạn. – birgire

+0

Tin tức từ năm 2017. Vẫn còn hoạt động trên Woocommerce 3.0+ và Wordpress 4.8 –

Các vấn đề liên quan