get the custom taxonomy name?

时间:2018-04-11 作者:tal

在我的functions.php 我有个钩子:

add_action( \'woocommerce_before_single_product\', \'display_category_list\',20 ); 

function display_category_list() {
  wc_get_template( \'woocommerce/single-product/single-product-top- 
       content.php\' );
 }
但我只需要在属于特定自定义分类法的产品页面上激活此挂钩。

所以我需要找出每个产品的自定义分类法。现在我有了这个代码:

  $taxonomy_objects = get_object_taxonomies( \'product\', \'objects\' );
   print_r( $taxonomy_objects);
这就是我得到的:

 Array ( [product_category] => WP_Taxonomy Object ( [name] => 
 product_category [label] => Product Categories [labels] => stdClass Object 
( [name] => Product Categories [singular_name] => Product Category 
[search_items] => Search Product Categories [popular_items] => [all_items] 
 => All Product Categories [parent_item] => Parent Product Category 
 [parent_item_colon] => Parent Product Category: [edit_item] => Edit Product 
  Category........
这就是我需要的

http://prntscr.com/j3o06n

我该怎么做?

1 个回复
SO网友:Bikash Waiba

您可以检查术语是否存在,see here

add_action( \'woocommerce_before_single_product\', \'display_category_list\',20 ); 

function display_category_list() {

  global $post;
  $term = \'term_name\'; // Term name string or array
  $taxonomy = \'product_cat\'; // your taxonomy

  if( has_term($term, $taxonomy, $post) ){ // if post belongs to term(s) 

     wc_get_template( \'woocommerce/single-product/single-product-top- 
       content.php\' );
  }
}

结束

相关推荐