alchymyth

one last post per child cat

Sep 11th, 2012
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.98 KB | None | 0 0
  1. <?php
  2.  
  3. $parent_cat = get_term_by('name', 'Blog', 'category');
  4. $child_cats = get_categories('child_of='.$parent_cat->term_id); //set the parent category
  5. $child_cats_ids = array();
  6. $counter = 0;
  7. if( $child_cats ) foreach( $child_cats as $cat ) { $child_cats_ids[] = $cat->term_id; } //get array of child cat IDs
  8.  
  9. $child_cat_posts = get_posts( array( 'category__in' => $child_cats_ids, 'posts_per_page' => -1 ) );  
  10.  
  11. if( $child_cat_posts ) foreach( $child_cat_posts as $post ) {
  12.     foreach( get_the_category($child_cat_post->ID) as $post_cat ) {
  13.         if( in_array( $post_cat->term_id, $child_cats_ids ) ) {
  14.             setup_postdata($post);
  15.             //start output of post//
  16.             the_title; echo ' - '; the_date(); echo ' - ' . $post_cat->name; echo '<br />';
  17.             //end of post output//
  18.             $key = array_search($post_cat->term_id,$child_cats_ids);
  19.             unset($child_cats_ids[$key]);
  20.             $counter++;
  21.             break;
  22.         }
  23.     if( $counter >= 6 ) break; //max number of posts
  24.     }
  25. wp_reset_postdata();
  26. }
  27.  
  28. ?>
Advertisement
Add Comment
Please, Sign In to add comment