custom category tree in wordpress
<?php
$args = array(
'taxonomy' => 'product-type',
'hierarchical' => true,
'title_li' => '',
'hide_empty' => false
);
?>
<ul>
<?php wp_list_categories( $args ); ?>
</ul>
You can use the wp_list_categories function also for taxonomies.
http://codex.wordpress.org/Template_Tags/wp_list_categories
https://wordpress.stackexchange.com/questions/39125/custom-taxonomy-tree-view
(works for any taxonomies,including "category")
$your_taxonomy='category';
function my_Categ_tree($TermName='', $termID, $separator='', $parent_shown=true ){
$args = 'hierarchical=1&taxonomy='.$TermName.'&hide_empty=0&orderby=id&parent=';
if ($parent_shown) {$term=get_term($termID , $TermName); $output=$separator.$term->name.'('.$term->term_id.')<br/>'; $parent_shown=false;}
$separator .= '-';
$terms = get_terms($TermName, $args . $termID);
if(count($terms)>0){
foreach ($terms as $term) {
//$selected = ($cat->term_id=="22") ? " selected": "";
//$output .= '<option value="'.$category->term_id.'" '.$selected .'>'.$separator.$category->cat_name.'</option>';
$output .= $separator.$term->name.'('.$term->term_id.')<br/>';
$output .= my_Categ_tree($TermName, $term->term_id, $separator, $parent_shown);
}
}
return $output;
}
Then you can output:
1) target category(taxonomy) tree, using specific ID
echo my_Categ_tree($your_taxonomy, 0 );
2) All categories/taxonomies
foreach (get_terms($your_taxonomy, array('hide_empty'=>0, 'parent'=>0)) as $each) {
echo my_Categ_tree($each->taxonomy,$each->term_id);
}
Thanks For Your Response , I Found the Solution : This Code Works Fine
$args = array(
'type' => 'post',
'child_of' => 0,
'parent' => 0,
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1,
'hierarchical' => 0,
'exclude' => '',
'include' => '',
'number' => '',
'taxonomy' => 'category',
'pad_counts' => false
);
$cats = get_categories( $args );
foreach( $cats as $cat) {
if($cat->parent == 0) {
$parent_cat = null;
$head = $cat->name;
$head_id = $cat->term_id;
}
echo "<ul><a class='parent-category' href=''>" . $head . "</a>";
wp_list_cats("sort_column=NAME&optioncount=0&hierarchical=1&hide_empty=0&child_of={$head_id}&show_option_none=");
echo "</ul>";
}