How to list categories and subcategories in JSON format?

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

Leave a Comment