Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
2  
Can't you build your custom array and do a json_encode? –  brasofilo Jan 17 '13 at 0:15
    
@brasofilo It will change (categories), how can I make custom array dynamically? –  Amino Jan 17 '13 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 Jan 17 '13 at 1:01
add comment

1 Answer

up vote 0 down vote accepted

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
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.