Edited (1)
您需要使用
\'child_of\'
从…起
WP_Term_Query
可用参数,这将允许您获取当前产品类别的所有子术语
parent 期限:
// 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 );
}
}
}
}
或者您也可以使用
get_term_children()
例如:
// 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>\';
}
}
这个
is only for product category archive pages, 至于产品页面,您可以为一个产品设置多个产品类别,代码将非常复杂,而且完全不同…