List subcategories in wordpress


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2. if(is_category()) {
  3.  
  4. $breakpoint = 0;
  5. $thiscat = get_term( get_query_var('cat') , 'category' );
  6. $subcategories = get_terms( 'category' , 'parent='.get_query_var('cat') );
  7.  
  8. if(empty($subcategories) && $thiscat->parent != 0) {
  9. $subcategories = get_terms( 'category' , 'parent='.$thiscat->parent.'' );
  10. }
  11.  
  12. $items='';
  13. if(!empty($subcategories)) {
  14. foreach($subcategories as $subcat) {
  15. if($thiscat->term_id == $subcat->term_id) $current = ' current-cat'; else $current = '';
  16. $items .= '
  17. <li class="cat-item cat-item-'.$subcat->term_id.$current.'">
  18. <a href="'.get_category_link( $subcat->term_id ).'" title="'.$subcat->description.'">'.$subcat->name.' ('.$subcat->count.')</a>
  19. </li>';
  20. }
  21. echo "<ul>$items</ul>";
  22. }
  23. unset($subcategories,$subcat,$thiscat,$items);
  24. }
  25. ?>

URL: http://wordpress.org/support/topic/showing-subcategories-of-a-parent-page-under-subcategory

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.