Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

custom category tree in wordpress

Tags:

php

wordpress

Hi i want to create a tree of categories in wordpress like this :

<ul>
    <a href=""> PARENT1 </a>
    <li><a href=""> CHILD 1-1</a></li>
    <li><a href=""> CHILD 1-2</a></li>
     .
     .
     .
</ul>
<ul>
    <a href=""> PARENT2 </a>
    <li><a href=""> CHILD 2-1</a></li>
    <li><a href=""> CHILD 2-2</a></li>
     .
     .
     .
</ul>

i want something that creates categories list in above format and show only categories which have children and hide those who don't

I Tried Something like this but it didn't give me what i wanted

<?php $args = array(
'type'                     => 'post',
'child_of'                 => 0,
'parent'                   => '',
'orderby'                  => 'name',
'order'                    => 'ASC',
'hide_empty'               => 0,
'hierarchical'             => 1,
'exclude'                  => '',
'include'                  => '',
'number'                   => '',
'taxonomy'                 => 'category',
'pad_counts'               => false 
);
$cats = get_categories( $args );
foreach( $cats as $cat) {
    if($cat->parent == 0) {
        $head = $cat->name;
        $cat_id = $cat->term_id;
    }
    echo "<a href=''>" . $head . "</a>";
    wp_list_cats("sort_column=NAME&optioncount=0&hierarchical=1&hide_empty=0&child_of={$cat_id}");
}

?>

like image 549
Amin Avatar asked Aug 23 '13 11:08

Amin


3 Answers

<?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

like image 81
Ravi Patel Avatar answered Sep 23 '22 19:09

Ravi Patel


(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);
}
like image 28
T.Todua Avatar answered Sep 26 '22 19:09

T.Todua


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>";
            }
like image 26
Amin Avatar answered Sep 26 '22 19:09

Amin