custom category tree in wordpress

10,957

Solution 1

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>";
            }

Solution 2

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

Solution 3

(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);
}

Solution 4

It's a very old post, but maybe some more clean code. For all the options of get_terms please see the docs. Feel free to make it more generic:

function build_custom_category_tree ($activeCatId, $activeParentId, $parentId = 0) {

    $output = '';
    $terms = get_terms( array(
        'taxonomy' => 'custom_categories',
        'hide_empty' => true,
        'hierarchical' => true,
        'parent' => $parentId
    ) );

    if (count($terms)) {

        $output .= '<ul>';

        foreach ($terms as $term) {
            $output .= '<li class="custom-cat' . ($term->term_id === $activeParentId || $term->term_id === $activeCatId ? ' active' : '') . '">';
            $output .=  $term->name;
            $output .=  build_custom_category_tree($activeCatId, $activeParentId, $term->term_id);
            $output .= '</li>';
        }

        $output .= '</ul>';
    }

    return $output;
}

And then in your template:

<div>
   <?= build_custom_category_tree($catIdOfPost, $rootIdOfCat) ?>
</div>
Share:
10,957

Related videos on Youtube

Amin
Author by

Amin

I am a full stack javascript developer, I also have years of experience in PHP and developing WordPress themes and plugins. My Portfolio

Updated on September 20, 2022

Comments

  • Amin
    Amin over 1 year

    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}");
    }
    

    ?>

  • Tony
    Tony about 10 years
    wp_list_cats is deprecated, use wp_list_categories