Don't like ads? PRO users don't see any ads ;-)
Guest

nested hierarchical category list

By: alchymyth on Feb 10th, 2012  |  syntax: PHP  |  size: 1.38 KB  |  hits: 274  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php      
  2. hierarchical_category_tree( 0 ); // the function call; 0 for all categories; or cat ID
  3.  
  4. function hierarchical_category_tree( $cat ) {
  5.         static $depth=0;    
  6.         // wpse-41548 // v1.3 // structured nested list // alchymyth // a hierarchical list of all categories //    
  7.         $next = get_categories('hide_empty=0&orderby=name&order=ASC&parent=' . $cat);    
  8.         if( $next ) :  
  9.                 $depth++;    
  10.                         echo "\n";      for($i=1;$i<$depth;$i++) echo '   '; //for structured html output    
  11.                 echo '<ul class="depth-'.$depth.'">';  
  12.                 foreach( $next as $cat ) :
  13.                         echo "\n ";     for($i=1;$i<$depth;$i++) echo '   '; //for structured html output    
  14.                 echo '<li><strong>' . $cat->name . '</strong>';    
  15.                 echo ' / <a href="' . get_category_link( $cat->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $cat->name ) . '" ' . '>View ( '. $cat->count . ' posts )</a>  ';      
  16.                 echo ' / <a href="'. get_admin_url().'edit-tags.php?action=edit&taxonomy=category&tag_ID='.$cat->term_id.'&post_type=post" title="Edit Category">Edit</a>';      
  17.                 hierarchical_category_tree( $cat->term_id );    
  18.                 echo '</li>';
  19.                 endforeach;
  20.                         echo "\n";      for($i=1;$i<$depth;$i++) echo '   '; //for structured html output    
  21.                 echo '</ul><!--/.depth-'.$depth.'-->';      
  22.                 $depth--;
  23.                         echo "\n ";     for($i=1;$i<$depth;$i++) echo '   '; //for structured html output      
  24.         endif;                 
  25. }  
  26. ?>