admin管理员组文章数量:1395785
In WooCommerce, I am trying to get all child product category terms from the parent term id of the current product category with the code below:
$category = get_queried_object();
$category_parent_id = $category->parent;
$category_test = get_terms( ['taxonomy' => 'product_cat', 'hide_empty' => false] );
The variable $category_parent_id
returns the parent product category id and $category_test
returns all product category terms
I'm trying to return all child product categories using the parent product category term id retrieved from $category->parent
Logically I tried:
$category_test = $category_parent_id->get_terms( ['taxonomy' => 'product_cat', 'hide_empty' => false] );
and
$category_test = $category->parent->get_terms( ['taxonomy' => 'product_cat', 'hide_empty' => false] );
But this didn't give me back all child categories of the current parent category. Anyone have any idea if and if so how this can be achieved?
In WooCommerce, I am trying to get all child product category terms from the parent term id of the current product category with the code below:
$category = get_queried_object();
$category_parent_id = $category->parent;
$category_test = get_terms( ['taxonomy' => 'product_cat', 'hide_empty' => false] );
The variable $category_parent_id
returns the parent product category id and $category_test
returns all product category terms
I'm trying to return all child product categories using the parent product category term id retrieved from $category->parent
Logically I tried:
$category_test = $category_parent_id->get_terms( ['taxonomy' => 'product_cat', 'hide_empty' => false] );
and
$category_test = $category->parent->get_terms( ['taxonomy' => 'product_cat', 'hide_empty' => false] );
But this didn't give me back all child categories of the current parent category. Anyone have any idea if and if so how this can be achieved?
Share Improve this question edited Mar 19, 2020 at 20:43 LoicTheAztec 3,39117 silver badges24 bronze badges asked Mar 19, 2020 at 17:46 Jeroen_LJeroen_L 431 gold badge1 silver badge5 bronze badges1 Answer
Reset to default 4Edited (1)
You will need to use 'child_of'
from WP_Term_Query
available arguments, that will let you to get all child terms for the current product category parent term:
// Only on product category archive pages
if( is_product_category() ) {
$main_term = get_queried_object();
$args_query = array(
'taxonomy' => 'product_cat',
'hide_empty' => false,
'child_of' => $main_term->parent
);
if ( $main_term->parent != 0 ) {
// Loop through WP_Term Objects
foreach ( get_terms( $args_query ) as $term ) {
if( $term->term_id != $main_term->term_id ) {
// $term->slug; // Slug
// Output each (linked) term name…
echo sprintf( '<a href="%s">%s</a></br>', get_term_link( $term->term_id, 'product_cat' ), $term->name );
}
}
}
}
Or you can also use get_term_children()
like:
// Only on product category archive pages
if( is_product_category() ) {
$main_term = get_queried_object();
$taxonomy = 'product_cat';
if ( $main_term->parent != 0 ) {
$child_ids = get_term_children( $main_term->parent, $taxonomy );
echo '<ul>';
foreach ( $child_ids as $child_id ) {
if( $child_id != $main_term->term_id ) {
$term = get_term_by( 'id', $child_id, $taxonomy );
echo '<li><a href="' . get_term_link( $child_id, $taxonomy ) . '">' . $term->name . '</a></li>';
}
}
echo '</ul>';
}
}
This is only for product category archive pages, as for product pages you can have many product categories set for a product and the code will be complicated and quiet different…
本文标签: custom taxonomyGet child product categories from parent product category in WooCommerce
版权声明:本文标题:custom taxonomy - Get child product categories from parent product category in WooCommerce 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744654618a2617888.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论