php – 多层次结构类别没有在wordpress分类中显示第三级层次结构
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – 多层次结构类别没有在wordpress分类中显示第三级层次结构,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2733字,纯文字阅读大概需要4分钟。
内容图文
我有自定义帖子类型的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;
}
和下拉显示如下:
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
解决方法:
你只需要更深层次地循环:
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;
if( !isset( $hierarchy[ $child->term_id ] ) )
continue;
foreach ($hierarchy[ $child->term_id ] as $subchild) {
$subchild = get_term( $subchild, $taxonomy );
$term_list[ $subchild->term_id ] = $term->name . ' > ' . $child->name. ' > ' .$subchild->name;
}
}
}
}
return $term_list;
}
内容总结
以上是互联网集市为您收集整理的php – 多层次结构类别没有在wordpress分类中显示第三级层次结构全部内容,希望文章能够帮你解决php – 多层次结构类别没有在wordpress分类中显示第三级层次结构所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。