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…
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744654618a4586104.html
评论列表(0条)