Advertisement
alchymyth

cats and posts

Mar 31st, 2011
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.20 KB | None | 0 0
  1. <?php
  2. /*****************************************************************
  3. * alchymyth 2011
  4. * www.transformationpowertools.com/wordpress
  5. *
  6. * a hierarchical list of all categories, with linked post titles
  7. *
  8. ******************************************************************/
  9. // http://codex.wordpress.org/Function_Reference/get_categories
  10.  
  11.  foreach( get_categories('hide_empty=false') as $cat ) :
  12.  if( !$cat->parent ) {
  13.  echo '<ul><li><strong>' . $cat->name . '</strong></li>';
  14.  process_cat_tree( $cat->term_id );
  15.  echo '</ul>';
  16.  }
  17.  endforeach;
  18.  
  19.  wp_reset_query(); //to reset all trouble done to the original query
  20. //
  21. function process_cat_tree( $cat ) {
  22.  
  23.  $args = array('category__in' => array( $cat ), 'numberposts' => -1);
  24.  $cat_posts = get_posts( $args );
  25.  
  26.  if( $cat_posts ) :
  27.  foreach( $cat_posts as $post ) :
  28.  echo '<li>';
  29.  echo '<a href="' . get_permalink( $post->ID ) . '">' . $post->post_title . '</a>';
  30.  echo '</li>';
  31.  endforeach;
  32.  endif;
  33.  
  34.  $next = get_categories('hide_empty=false&parent=' . $cat);
  35.  
  36.  if( $next ) :
  37.  foreach( $next as $cat ) :
  38.  echo '<ul><li><strong>' . $cat->name . '</strong></li>';
  39.  process_cat_tree( $cat->term_id );
  40.  endforeach;
  41.  endif;
  42.  
  43.  echo '</ul>';
  44. }
  45. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement