如何从当前父类别中获取 Woocommerce 所有子类别

我希望当我点击它们时,我在一个页面上有一些产品类别,然后我应该得到她孩子的类别。我希望这项工作应该在单页上完成,而不是多页 我该如何做这项工作


哈士奇WWW
浏览 238回答 2
2回答

米琪卡哇伊

您可以使用get_term_children()来自产品类别术语 Id 的WordPress 专用功能:$child_terms_ids = get_term_children( $term_id, 'product_cat' );它返回一组子项 Id。对于产品类别存档页面,您可以使用:// get the current product category term IDif ( is_product_category() ) {    $term_id = (int) get_queried_object_id();}if ( term_exists( $term_id, 'product_cat' ) ) {    $child_terms_ids = get_term_children( $term_id, 'product_cat' );    // Loop through child terms Ids    foreach ( $child_terms_ids as $child_term_id ) {        $child_term = get_term_by( 'term_id', $child_term_id, 'product_cat' );        // The term name        $child_name = $child_term->name;        // The term link        $child_link = get_term_link( $child_term, 'product_cat' );    }}
打开App,查看更多内容
随时随地看视频慕课网APP