• I want a multi-dimensional array of my categories.

    function get_category_array()
    {
    	$args = array(
    		'type'                     => 'post',
    		'child_of'                 => 0,
    		'hide_empty'               => false,
    		'hierarchical'             => true,
    		'exclude'                  => '1'
    	);
    
    	return get_categories($args);
    }

    The above function is returning a “flat” list of all of the categories like this (instead of a parent->child->grandchild->etc.. array)

    Array
    (
        [0] => stdClass Object
            (
                [term_id] => 210
                [name] => Cat 1 Name
                [slug] => cat-1-name
                [term_group] => 0
                [term_taxonomy_id] => 211
                [taxonomy] => category
                [description] =>
                [parent] => 208
                [count] => 0
                [cat_ID] => 210
                [category_count] => 0
                [category_description] =>
                [cat_name] => Cat 1 Name
                [category_nicename] => cat-1-name
                [category_parent] => 208
            )
    
        [1] => stdClass Object
            (
                [term_id] => 234
                [name] => Cat 2 Name
                [slug] => cat-2-name
                [term_group] => 0
                [term_taxonomy_id] => 237
                [taxonomy] => category
                [description] =>
                [parent] => 167
                [count] => 0
                [cat_ID] => 234
                [category_count] => 0
                [category_description] =>
                [cat_name] => Cat 2 Name
                [category_nicename] => cat-2-name
                [category_parent] => 167
            )
    )

    Anybody know why this isn’t working?

Viewing 3 replies - 1 through 3 (of 3 total)
  • I think get_categories is working correctly. It just didn’t return the data in the way that you expected. The hierarchical parameter, as far as I can tell, determines whether the function should return data for empty categories. It doesn’t return the data in a multi-dimensional (hierarchical) array. And, yes, I think the parameter name is mis-leading too.

    I suspect you’ll need to build your array manually – using get_category_parents and/or cat_is_ancestor_of. Unless, of course, someone else comes along and posts a simple solution comprising half a dozen beautiful lines. ??

    here you go ??

    function get_cat_hierchy($parent,$args){
    
    	$cats = get_categories($args);
    	$ret = new stdClass;
    
    	foreach($cats as $cat){
    		if($cat->parent==$parent){
    			$id = $cat->cat_ID;
    			$ret->$id = $cat;
    			$ret->$id->children = get_cat_hierchy($id,$args);
    		}
    	}
    
    	return $ret;
    }

    … for those non coders call the function like this

    $args = array(
    		'type'                     => 'post',
    		'child_of'                 => 0,
    		'hide_empty'               => false,
    		'hierarchical'             => true,
    		'exclude'                  => '1'
    	);

    or whatever args you need as per get_categories func

    and pass those and the id of the root category you want, and viola.

    eg.
    print_r(get_cat_hierchy(0,$args))
    will print out all cats

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘get_categories hierarchy not working’ is closed to new replies.