尝试这个 :
<?php
global $post, $product;
$term_list = wp_get_post_terms($post->ID, 'product_cat', array("fields" => "all")); //get array containing category details
$cat_id = ($term_list[0]->parent); //get parent category ID from the above generated array
$cat_url = get_term_link ($cat_id, 'product_cat'); //get link of parent ID
$term = get_term( $cat_id, 'product_cat' ); //Get Name of the parent from the parent ID
$name = $term->name; //Store it into an varialbe
echo "Artist: <a href='".esc_url($cat_url)."'>".$name."</a>";
?>
记住 :
In WooCommerce
,产品类别不正常类别,他们是一个自定义分类法专门为products它只是标记为「类别」.
如果您有任何疑问,请告诉我。
Updated:
<?php
global $post, $product;
$term_list = wp_get_post_terms($post->ID, 'product_cat', array("fields" => "all")); //get array containing category details
$cat_id = ($term_list[0]->parent); //get parent category ID from the above generated array
$termchildren = get_term_children( '90' , 'product_cat' ); //New Line in Updattion -1
$new_cat_id = $termchildren[2];
$cat_url = get_term_link ($new_cat_id, 'product_cat'); //get link of parent ID
$term = get_term( $new_cat_id, 'product_cat' ); //Get Name of the parent from the parent ID
$name = $term->name; //Store it into an varialbe
echo "Artist: <a href='".esc_url($cat_url)."'>".$name."</a>";
?>
新更新(2015 年 1 月 2 日)
<?php
global $post, $product;
$cat_array = array();
$term_list = wp_get_post_terms($post->ID, 'product_cat', array("fields" => "all")); //get array containing category details
foreach($term_list as $cat_list)
{
array_push($cat_array, $cat_list->term_id);
}
$cat_id = ($term_list[0]->parent); //get parent category ID from the above generated array
$termchildren = get_term_children( '90' , 'product_cat' ); //New Line in Updattion -1
$final_result = array_intersect($cat_array,$termchildren);
$final_cat_result = array_values($final_result);
$new_cat_id = $final_cat_result[0];
$cat_url = get_term_link ($new_cat_id, 'product_cat'); //get link of parent ID
$term = get_term( $new_cat_id, 'product_cat' ); //Get Name of the parent from the parent ID
$name = $term->name; //Store it into an varialbe
echo "Artist: <a href='".esc_url($cat_url)."'>".$name."</a>";
?>
Line : 1 $post
and $product
两者都是全局变量因此,为了在其他文件中使用它,我们需要在使用它之前将其添加到我们的文件中。
Line : 2 One 空白数组存储当前产品的所有类别**。我们将来会使用它。
Line : 3 wp_get_post_terms http://codex.wordpress.org/Function_Reference/wp_get_post_terms#Examples习惯于检索帖子的条款(对于 woocommerce 来说,它是产品类别)。所以现在我们有一个数组,其中包含术语的所有详细信息ID, name
etc etc
Line : 4它用于循环遍历上面生成的数组。我们将循环遍历一个数组并查找term_id
.我们将使用array_push http://php.net/manual/en/function.array-push.php为了存储所有术语 ID,为了存储,我们将使用第 2 行中的空白数组。所以现在我们有一个数组term_id
.
Line : 9现在我们将使用get_term_children http://codex.wordpress.org/Function_Reference/get_term_children#Examples检索子项Artist据我们所知这位艺术家term ID
并且它是固定的。它将给出一个array作为输出。
线路:10 array_intersect http://php.net/manual/en/function.array-intersect.php对于匹配两个数组并仅取出匹配的值非常有用。(基本上我们正在查看当前的产品类别和所有艺术家类别以仅取出匹配的类别)。
线路:11 array_values http://php.net/manual/en/function.array-values.php对于重新索引数组很有用。(通过添加这一行,我们解决了即将出现的错误:))
线路:12现在我们有一个数组,其中只有one艺术家的价值term ID
(就是这样。现在您只需要从该术语 ID 中获取艺术家的姓名和链接)
线路:13获取艺术家的链接。
线路:15从第 14 行生成的数组中获取艺术家的姓名并将其存储在变量中。
线路:16打印所需的内容,我们就完成了!