Advertisement
alchymyth

hierachical category listing, process_cat_tree() function

Jan 6th, 2012
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.16 KB | None | 0 0
  1. /************************************************************************
  2. * a function to recursively build a hierachical category listing;
  3. * alchymyth 01/2012
  4. * specialy adapted to work with these accordion scripts:
  5. * /themes/sztyle/mockup/style.css" />
  6. * /themes/sztyle/mockup/jquery-1.4.2.min.js"></script>
  7. * /themes/sztyle/mockup/menu.js"></script>
  8. *************************************************************************/
  9.  
  10. function process_cat_tree( $cat ) {
  11.  global $level; //set depth level
  12.  
  13.  $args = array(
  14.     'category__in' => array( $cat ),
  15.     'numberposts' => -1,
  16.     'orderby' => 'date', // possible orderby values: 'date', 'title', 'modified', etc - see http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
  17.     'order' => 'DESC');
  18. // full query parameter list, see http://codex.wordpress.org/Class_Reference/WP_Query#Parameters
  19.  
  20.  $cat_posts = get_posts( $args );
  21.  
  22.  $next = get_categories('hide_empty=0&orderby=slug&parent=' . $cat); //fix for posts in cats with sub cats
  23.  
  24.  if( $cat_posts ) : //fix for posts in cats with sub cats
  25.  
  26. if(!$next) echo "\n   ".'<ul class="acitem indent">';//fix for posts in cats with sub cats
  27. if($next) echo "\n   ".'<ul class="menu nestedmenu noaccordion acitem indent">';
  28.  
  29.  foreach( $cat_posts as $post ) :  
  30.  echo "\n      ".'<li>';
  31.  
  32. //BEGIN post output section//
  33.  
  34.  
  35.  $post_thumb = get_the_post_thumbnail($post->ID); //get the post thumbnail - featured image
  36.  $post_link = get_post_meta($post->ID,'_custom_featured_link',true); //get the link url from the custom field
  37.  
  38.  echo '<div class="postcontent">' . "\n";
  39.  if($post_link) echo '<a href="'. $post_link  .'" target="_blank">'; //link tag for post
  40.  if( $post_thumb ) echo $post_thumb . ' '; // code implemented to show thumbnail if exists
  41.  echo $post->post_title; //post title
  42.  if($post_link) echo '</a>'; //close link
  43.  
  44.  
  45.  echo apply_filters('the_content',$post->post_content); // in case the post has some content
  46.  echo "\n" . '</div><!--.postcontent-->';
  47.  
  48. //END post output section//
  49.  
  50.  echo "\n      ".'</li>';
  51.  endforeach;
  52.  
  53. if(!$next) echo "\n   ".'</ul><!--/.acitem indent-->';//fix for posts in cats with sub cats
  54.  endif; // end of if( $cat_posts ) :
  55.  
  56.  $next = get_categories('hide_empty=0&parent=' . $cat);
  57.  $count = 0;
  58.  if( $next ) :
  59.   $level++; //increase depth level
  60.  
  61.     if($level == 2) echo "\n".'<ul class="menu noaccordion">'; 
  62.     if(!$cat_posts && $level > 2) echo "\n   ".'<ul class="menu nestedmenu noaccordion acitem indent">'; //fix for posts in cats with sub cats
  63.     $cat_posts = ''; //fix for posts in cats with sub cats
  64.    
  65.  foreach( $next as $cat ) :
  66.  $count++;
  67.     if($level == 2) echo "\n".'<li>';
  68.     if($level > 2) echo "\n   ".'<li>';
  69.  
  70.  echo "\n".'<a href="#" class="level'. ($level-1) .'">'. $cat->name .'</a>';
  71.  
  72.  process_cat_tree( $cat->term_id );
  73.  
  74.     if($level == 2) echo "\n".'</li>';
  75.     if($level > 2) echo "\n   ".'</li>';
  76.  
  77.  endforeach;
  78.   $level--; //decrease depth level
  79.  
  80.     if($level == 1) echo "\n".'</ul><!--/.menu noaccordion-->';
  81.     if($level > 1) echo "\n   ".'</ul><!--/.menu nestedmenu noaccordion acitem indent-->';
  82. endif; //end of  if( $next )
  83. }
  84. //END OF function process_cat_tree( $cat )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement