Advertisement
Guest User

subcategorytitlerepeats

a guest
Apr 14th, 2014
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.09 KB | None | 0 0
  1. <!-- TEST -->
  2.  start test
  3.  
  4. <?php
  5. //get all child categories for category 6, then for each child category display the posts
  6.  
  7. $parent = get_category($cat->category_parent);
  8. $parent_cat = $parent->cat_ID;
  9. $taxonomy = 'category';
  10. $cat_children = get_term_children($parent_cat, $taxonomy );
  11.  
  12. // for each subcategory store in array
  13.     foreach($cat_children as $subcategory)
  14.     {
  15.     $args=array(
  16.       'cat' => $subcategory,
  17.       'post_type' => 'post',
  18.     );
  19.    
  20. // print out category id to double check
  21.     echo $subcategory;
  22.  
  23. // the query   
  24.      $my_query = null;
  25.      $my_query = new WP_Query($args);
  26.  
  27.     if( $my_query->have_posts() )
  28.     {
  29.         foreach( get_the_category() as $subcategory)
  30.         {    
  31. // post subcategory title      
  32.     echo  $subcategory->name;  
  33.      
  34. // post items of each category
  35.           while ($my_query->have_posts()) : $my_query->the_post(); ?>
  36.          
  37.                <p><?php the_title(); ?></p>
  38.         <?php
  39.  
  40.       endwhile;
  41.      
  42.       }  
  43.     }
  44.   }
  45.   // Restore global post data stomped by the_post().  
  46.   wp_reset_query();
  47.  
  48. ?>
  49.  
  50.      
  51. endtest
  52. <!-- END TEST -->
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement