woocommerce功能get_categories()
在中声明abstract-wc-product.php
, 因为它基于wordpress函数get_the_term_list()
无法仅获取类别的特定分支。这绝对不同于wordpress函数get_categories()
, 通过它的使用方式,可以看出它是woocommerce特有的$product->get_categories()
. 当然,除了可以使用的论点之间的明显差异之外,您还可以更深入地阅读和比较链接的信息
典型的分类轴系树(categoryTaxonomy tree):
product_cat
|
|––cat1
| |––child1.1
| |––child1.2
| |––child1.3
|
|––cat2
|––child2.1
|––child2.2
|––child2.3
在您的情况下,分支将是»artist«和»art«,而不是»cat1«和»cat2«。目标是只获取要显示的标题的一个相关类别分支。至少下面的代码将实现这一点。
正如我上面所说的,如果您想实现仅从类别/分类法的特定分支获取术语,您必须以不同的方式来实现。你必须利用get_terms()
和wp_get_post_terms()
然后将结果相交。
//woocommerce product category
$taxonomies = array( \'product_cat\' );
//let\'s get an array with ids of all the terms a post has
$post_tids = wp_get_post_terms($post->ID, \'product_cat\', array("fields" => "ids"));
//you have to change branch parent id accordingly
$id_branch_parent = \'111\';
$args = array(
\'fields\' => \'ids\',
\'child_of\' => $id_branch_parent
);
//let\'s get an array with ids of all the terms a the branch has
$branch_tids = get_terms( $taxonomies, $args );
//intersect post and branch terms to get an array of common ids
$pnb_tids = array_intersect($post_tids, $branch_tids);
//in case of multiple ids we have to loop through them
foreach ( $pnb_tids as $tid ) {
//get the term information by id
$tobj = get_term_by(\'id\', $tid, \'product_cat\');
//store the term names into an array
$pnb_name_arr[] = $tobj->name;
}
//implode the name array, this is the result you want to echo
$pnb_term_list = implode(\' – \', $pnb_name_arr);
这应该会给你想要的结果,但它还没有经过测试。