Advertisement
alchymyth

1post per sub-cat

Jun 22nd, 2011
509
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.53 KB | None | 0 0
  1. <?php //in a category archive, show one post per sub category, no duplicates, in three columns
  2. //alchymyth 2011
  3.  
  4. $cat = get_query_var('cat');
  5. $no_duplicates = array();
  6.  
  7. $sub_cats = get_categories('parent='.$cat); //get the direct sub categories
  8.   if( $sub_cats ) :
  9.  
  10.     foreach( $sub_cats as $sub_cat ) :
  11.  
  12.     /*get the latest post of this subcat;
  13.     for instance: output the category name, the post title, and the excerpt of one post*/
  14.     $args = array(
  15.     'posts_per_page' =>1,
  16.     'category__in' => array($sub_cat->term_id),
  17.     'post__not_in' => $no_duplicates
  18.     );
  19.     $cat_query = new WP_Query( $args );
  20.         if( $cat_query->have_posts() ) : while( $cat_query->have_posts() ) :
  21.         $cat_query->the_post();
  22.         $no_duplicates[] = $post->ID; ?>
  23.  
  24.         <?php $column = ($column == '') ? 'columnleft' : (($column == 'columnleft') ? 'columnmiddle' : (($column == 'columnmiddle') ? 'columnright' : 'columnleft' )); // define the css class for the respective column ?>
  25.         <div class="<?php echo $column; ?>">
  26.             <h3>
  27.         <a href="<?php echo get_category_link($sub_cat->term_id); ?>" title="View all posts filed under <?php echo $sub_cat->name; ?>"><?php echo $sub_cat->name; ?></a>
  28.         </h3>
  29.      
  30.         <a href="<?php the_permalink(); ?>" rel="bookmark" title=" <?php the_title_attribute(); ?>" class="title"><?php the_title(); ?></a>
  31.        
  32.         <?php the_excerpt(); ?>
  33.  
  34.         </div>
  35.         <!-- END <?php echo $column; ?> COLUMN -->
  36.  
  37.     <?php endwhile; endif; ?>
  38.  
  39. <?php endforeach; //ends: foreach( $sub_cats as $sub_cat )
  40.  
  41.   endif; //ends: if( $sub_cats ) ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement