我正在修改一个主题以使用父分类术语而不是所有分类术语,现在我只能显示父分类术语,但现在我遇到了计数问题。
基本上,这是必须更改的代码
$_products_in_term = get_objects_in_term( $term->term_id, $taxonomy );
这将获取当前项的所有项,然后使用当前项进行截取,然后进行计数。现在,我如何修改这段代码,使其不仅能获得当前学期的学期,而且如果它有孩子,也能获得其所有孩子的学期,(只会深入一步,不会与孙子一起工作)
这是代码完整的代码
foreach ($terms as $term) {
// Get count based on current view - uses transients
$transient_name = \'wc_ln_count_\' . md5( sanitize_key($taxonomy) . sanitize_key( $term->term_id ) );
if ( false === ( $_products_in_term = get_transient( $transient_name ) ) ) {
// $_products_in_term = get_objects_in_term( $term->term_id, $taxonomy );
$args2[\'child_of\'] = $term->term_id;
$terms = get_terms( $taxonomy , $args2 );
$term_array=array();
foreach($terms as $terms){
$term_array[]= $terms->term_id;
}
$_products_in_term = get_objects_in_term( $term_array, $taxonomy );
set_transient( $transient_name, $_products_in_term );
}
$option_is_set = (isset($_chosen_attributes[$taxonomy]) && in_array($term->term_id, $_chosen_attributes[$taxonomy][\'terms\'])) ;
// If this is an AND query, only show options with count > 0
if ($query_type==\'and\') {
$count = sizeof(array_intersect($_products_in_term, $woocommerce->query->filtered_product_ids));
// skip the term for the current archive
if ( $current_term == $term->term_id ) continue;
if ($count>0 && $current_term !== $term->term_id ) $found = true;
if ($count==0 && !$option_is_set) continue;
// If this is an OR query, show all options so search can be expanded
} else {
// skip the term for the current archive
if ( $current_term == $term->term_id ) continue;
$count = sizeof(array_intersect($_products_in_term, $woocommerce->query->unfiltered_product_ids));
if ($count>0) $found = true;
}
最合适的回答,由SO网友:Rajeev Vyas 整理而成
$terms = get_term_children( $term->term_id , $taxonomy );
$term_array=array();
foreach($terms as $terms){
$term_array[]= $terms;
}
$_products_in_term = get_objects_in_term( $term_array, $taxonomy );