php提示最多三级分类,php – 多层次结构类别没有在wordpress分类中显示第三级层次结构...

在WordPress中,使用CMB2插件创建多级分类下拉菜单时遇到问题,代码只能显示两级分类,未能展示完整的三级分类结构。预期应显示Category1的子子类别和addoncategory,但实际输出中缺失了这一部分。
摘要由CSDN通过智能技术生成

我有自定义帖子类型的3个级别的类别,类别,子类别和子子类别.我试图使用cmb2在下拉列表中显示这些类别,我的代码只显示2个级别的类别和缺少第三级.

Category 1

-- child category 1

-- child category 2

-- addon category 1

-- addon category 2

-- child category 3

-- child category 4

-- addon category 1

-- addon category 2

Category 2

-- child category 1

-- child category 2

-- addon category 1

-- addon category 2

-- child category 3

-- child category 4

-- addon category 1

-- addon category 2

我正在使用cmb2在select2中使用multiselect选项获取这些类别.

并记下以下代码:

function gp_get_cmb_options_array_tax( $taxonomy, $args = array() ) {

if ( empty( $taxonomy ) ) { return; }

$defaults = array(

'hide_empty' => 0,

);

$args = wp_parse_args( $args, $defaults );

$terms = get_terms( $taxonomy, $args );

/**

* https://developer.wordpress.org/reference/functions/_get_term_hierarchy/

*/

$hierarchy = _get_term_hierarchy( $taxonomy );

$term_list = array();

foreach ( $terms as $term ) {

if( $term->parent ) {

continue;

}

$term_list[ $term->term_id ] = $term->name;

if( isset( $hierarchy[ $term->term_id ] ) ) {

foreach ( $hierarchy[ $term->term_id ] as $child ) {

$child = get_term( $child, $taxonomy );

$term_list[ $child->term_id ] = $term->name . ' > ' . $child->name;

}

}

}

return $term_list;

}

MUheL.png

和下拉显示如下:

Category 1

Category 1 > child category 1

Category 1 > child category 2

Category 1 > child category 3

Category 1 > child category 4

Category 2

Category 2 > child category 1

Category 2 > child category 2

Category 2 > child category 3

Category 2 > child category 4

虽然应该表现得像

Category 1

Category 1 > child category 1

Category 1 > child category 2

Category 1 > child category 2 > addon category 1

Category 1 > child category 2 > addon category 2

Category 1 > child category 3

Category 1 > child category 4

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值