Advertisement
Guest User

Wordpress show only subcategories from current parentrent

a guest
Jun 8th, 2012
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.08 KB | None | 0 0
  1. //************************ BEGIN CAT FUNCTION
  2.  
  3. function get_mymenu($curcat = 0, $depth = 1)
  4. {
  5.     $current_cat ="";
  6.     $breadcats = my_get_breadcats($curcat);
  7.     $my_cats = my_get_all_categories();
  8.     if($depth == 0){$listlevel = "parentlist";}
  9.     else{$listlevel = "childlists";}
  10.        
  11.     $html = "$parentid<ul class=\"".$listlevel."\">";
  12.     $categories = $my_cats[$breadcats[$depth]];
  13.     if(isset($categories))
  14.     {
  15.         foreach ($categories as $cat)
  16.         {
  17.             $parent ="";
  18.             $parentid = $cat->category_parent;
  19.             $catid = $cat->cat_ID;
  20.            
  21.             if($depth == 1){$level = "levelone";}
  22.             elseif($depth == 0){$level = "levelzero";}
  23.             elseif($depth == 2){$level = "leveltwo";}
  24.             elseif($depth == 3){$level = "levelthree";}
  25.             else{$level = "";}
  26.            
  27.             if($curcat == $cat->cat_ID){$current_subcat = "current_subcat";}
  28.             else{$current_subcat = "";}
  29.            
  30.             $anc = get_ancestors($curcat,'category');
  31.             foreach($anc as $ancestor){
  32.                 if($ancestor == $catid) {
  33.                     $parent = "current-cat-parent";
  34.                 }
  35.             }
  36.  
  37.             $html.= "<li class=\"cat-item ".$current_subcat." ".$level." ".$parent."\"><a href=\"". get_category_link( $cat->cat_ID ) ."\">". $cat->cat_name ."</a></li>";
  38.             if ($cat->cat_ID == $breadcats[$depth+1] && $depth<count($breadcats))
  39.             {
  40.                 $html.= get_mymenu($curcat, $depth+1);
  41.             }
  42.             $html.= "</li>\n";
  43.         }
  44.        
  45.     }
  46.     $html.= "</ul>\n";
  47.     return $html;
  48. }
  49.  
  50.  
  51. function my_get_all_categories()
  52. {
  53.     static $my_cats = array();
  54.     if (count($my_cats)==0)
  55.     {
  56.         $categories = get_categories('child_of=0', 'orderby=name');
  57.         foreach ($categories as $cat)
  58.         {
  59.             $my_cats[$cat->category_parent][$cat->cat_ID] = $cat;
  60.         }
  61.     }
  62.     return $my_cats;
  63. }
  64.  
  65. function my_get_breadcats($curcat = 0)
  66. {
  67.     global $post;
  68.     static $breadcats = array();
  69.     if (count($breadcats)==0)
  70.     {
  71.         $curcat = intval($curcat);
  72.         $breadcats[] = $curcat;
  73.         while($curcat!=0)
  74.         {
  75.             $curcategory = get_category($curcat);
  76.             $curcat = intval($curcategory->category_parent);
  77.             $breadcats[] = $curcat;
  78.         }
  79.         $breadcats = array_reverse($breadcats);
  80.     }
  81.     return $breadcats;
  82. }
  83.  
  84. //************************ END CAT FUNCTION
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement