Advertisement
Digitalraindrops

Category Menu Ancestors

Jun 1st, 2011
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.19 KB | None | 0 0
  1. if ( ! function_exists( 'digimag_top_categories' ) ) :
  2. function digimag_top_categories() {
  3.     $list_args = 'sort_column=name&sort_order=asc&style=list&depth=0&hierarchical=true&title_li=0&hide_empty=1&echo=0';
  4.     $catlist = wp_list_categories($list_args);
  5.     if ( is_category() ) {
  6.         //Get the active category id
  7.         global $cat;
  8.         $curr_cat = get_category($cat);
  9.         $catid = $curr_cat->cat_ID;
  10.         //Find the top level id and number of levels   
  11.         while ($catid){  
  12.             $curr_cat = get_category($catid);
  13.             $catid = $curr_cat->category_parent;
  14.             $id[] = $curr_cat->cat_ID;         
  15.         }
  16.         //How many levels more than two set hierarchical ancestor?
  17.         //Count from 1 array from 0 : 1:0=Current 2:1=Parent >2:1 all Ancestors
  18.         if( count($id) > 2){
  19.             $max=count($id)-1; //Array elements zero based = count - 1
  20.             $extra_class='current-cat-ancestor';
  21.             for ( $counter = 1; $counter <= $max; $counter ++) {
  22.                 $cat_ancestor_class = 'cat-item cat-item-'. $id[$counter];
  23.                 $amended_class = $cat_ancestor_class . ' ' . $extra_class;
  24.                 $catlist = str_replace($cat_ancestor_class, $amended_class, $catlist );
  25.             }
  26.         }
  27.     }
  28.     $menu = str_replace( array( "\r", "\n", "\t" ), '', $catlist );
  29.     echo $menu;
  30. }
  31. endif;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement