$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>How to list categories and subcategories in JSON format?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How to list categories and subcategories in JSON format?

matteradmin8PV0评论

I need to integrate this plugin with my WordPress site, and the categories must be in this format:

 "Option 1": {"Suboption":200},
 "Option 2": {"Suboption 2": {"Subsub 1":201, "Subsub 2":202},
                "Suboption 3": {"Subsub 3":203, "Subsub 4":204, "Subsub 5":205}
               }

How can I get that?
I tried the options of the json-api.

And this is the walker:

class MyWalker extends Walker_Category {
    var $tree_type = 'category';

    var $db_fields = array ('parent' => 'parent', 'id' => 'term_id');

    function start_lvl( &$output, $depth, $args = array() ) {
        if ( 'list' != $args['style'] )
            return;

        $indent = str_repeat("\t", $depth);
        $output .= "$indent:{";
    }

    function end_lvl( &$output, $depth, $args = array() ) {
        if ( 'list' != $args['style'] )
            return;

        $indent = str_repeat("\t", $depth);
        $output .= "$indent}\n";
    }

    function start_el($output, $category, $depth , $args = array() ) {
        extract($args);
        $cat_name = esc_attr( $category->name );
        $cat_name = apply_filters( 'list_cats', $cat_name, $category );
        $output .= '"' . $cat_name.'=>'.$depth . '",';
    }

    function end_el($output, $page, $depth = 0, $args = array() ) {
        if ( 'list' != $args['style'] )
            return;

        $output .= "\n";
    }
}

I need to integrate this plugin with my WordPress site, and the categories must be in this format:

 "Option 1": {"Suboption":200},
 "Option 2": {"Suboption 2": {"Subsub 1":201, "Subsub 2":202},
                "Suboption 3": {"Subsub 3":203, "Subsub 4":204, "Subsub 5":205}
               }

How can I get that?
I tried the options of the json-api.

And this is the walker:

class MyWalker extends Walker_Category {
    var $tree_type = 'category';

    var $db_fields = array ('parent' => 'parent', 'id' => 'term_id');

    function start_lvl( &$output, $depth, $args = array() ) {
        if ( 'list' != $args['style'] )
            return;

        $indent = str_repeat("\t", $depth);
        $output .= "$indent:{";
    }

    function end_lvl( &$output, $depth, $args = array() ) {
        if ( 'list' != $args['style'] )
            return;

        $indent = str_repeat("\t", $depth);
        $output .= "$indent}\n";
    }

    function start_el($output, $category, $depth , $args = array() ) {
        extract($args);
        $cat_name = esc_attr( $category->name );
        $cat_name = apply_filters( 'list_cats', $cat_name, $category );
        $output .= '"' . $cat_name.'=>'.$depth . '",';
    }

    function end_el($output, $page, $depth = 0, $args = array() ) {
        if ( 'list' != $args['style'] )
            return;

        $output .= "\n";
    }
}
Share Improve this question edited Jan 17, 2013 at 0:54 brasofilo 22.2k8 gold badges70 silver badges270 bronze badges asked Jan 16, 2013 at 23:41 AminoAmino 3232 gold badges5 silver badges17 bronze badges 3
  • 2 Can't you build your custom array and do a json_encode? – brasofilo Commented Jan 17, 2013 at 0:15
  • @brasofilo It will change (categories), how can I make custom array dynamically? – Amino Commented Jan 17, 2013 at 0:33
  • I really don't understand what is the heart of the problem, what does that jQuery Option Tree has to do with the JsonAPI plugin or the Walker_Category... I think, in general sense, every line of code is dynamic, so once again: check the PHP Manual. – brasofilo Commented Jan 17, 2013 at 1:01
Add a comment  | 

2 Answers 2

Reset to default 0

Here is a fast example using get_categories instead of a plugin or the walker class, maybe it will help you in the right direction, it will list all children of a parent category in json format. As brasofilo mentions since you require a specific format you will want to build a custom array.

// let's get the bare minimum going
$args = array(
    'type'                     => 'post',
    'child_of'                 =>  20, //change this, hardcoded as example
    'taxonomy'                 =>  'category'
);

$categories = get_categories( $args );
$json = json_encode($categories);

var_dump($json); //do what you want
$args = [
    'taxonomy' => 'category',
    'hide_empty' => 0,
    'parent' => 0
];

function _get_child_terms( $items ) {
    foreach ( $items as $item ) {
      $item->children = get_terms( 'category', array( 'child_of' => $item->term_id, 'hide_empty' => 0 ) );
      if ( $item->children ) _get_child_terms( $item->children );
    }
    return $items;
}

$terms = _get_child_terms( get_terms( $args ) );
echo json_encode( $terms );
Post a comment

comment list (0)

  1. No comments so far