php - 多层次类别未在 wordpress 分类法中显示第三级层次结构

标签 php wordpress cmb2

对于自定义帖子类型,我有 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 中获取这些类别。

并写下以下代码:

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;

}

showing only 2 levels and missing third level of categories

下拉菜单显示如下:

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 分类法中显示第三级层次结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47324350/

相关文章:

php - 使用代码覆盖率运行 PHPUnit 时出现 "Cannot redeclare class"错误

php - CMB2 选项页面参数

jquery - 使用 jQuery.load 时防止 div 折叠

php - 保存所有发布数据和发布元数据后会触发哪个 WordPress Hook ?

php - 当要比较的元数据是序列化数组时,Wordpress meta_query 值数组?

php - 导出到csv时如何获取特定的列名称?

php - get_page_children() 不返回所有子页面

php - Android - 使用 mysql.php 注册/登录表单

wordpress - 在 WordPress 上提交评论后出现自定义 'success' 消息

php - 用于制作 wordpress 主题的内联 css