为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
哈士奇WWW 2021-06-30 18:57:44
我希望当我点击它们时,我在一个页面上有一些产品类别,然后我应该得到她孩子的类别。我希望这项工作应该在单页上完成,而不是多页 我该如何做这项工作
查看完整描述

2 回答

?
米琪卡哇伊

TA贡献1998条经验 获得超6个赞

您可以使用get_term_children()来自产品类别术语 Id 的WordPress 专用功能:

$child_terms_ids = get_term_children( $term_id, 'product_cat' );

它返回一组子项 Id。


对于产品类别存档页面,您可以使用:


// get the current product category term ID

if ( 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' );

    }

}


查看完整回答
反对 回复 2021-07-02
  • 2 回答
  • 0 关注
  • 232 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信