Advertisement
Guest User

List posts by category in Wordpress

a guest
Feb 7th, 2012
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.93 KB | None | 0 0
  1. <?php
  2. //get all children of category "MyCat", then display posts in each cat
  3. $taxonomy = 'category';
  4. $param_type = 'category__in';
  5. $cat_id = get_cat_ID('MyCat');
  6. $term_args=array(
  7.   'orderby' => 'name',
  8.   'order' => 'ASC',
  9.   'child_of' => $cat_id
  10. );
  11. $terms = get_terms($taxonomy,$term_args);
  12. if ($terms) {
  13.   foreach( $terms as $term ) {
  14.     $args=array(
  15.       "$param_type" => array($term->term_id),
  16.       'post_type' => 'post',
  17.       'post_status' => 'publish',
  18.       'posts_per_page' => -1,
  19.       'caller_get_posts'=> 1
  20.       );
  21.     $my_query = null;
  22.     $my_query = new WP_Query($args);
  23.     if( $my_query->have_posts() ) {
  24.       echo '<ul>
  25.         <li class="listheader"><h2>'.$term->name.'</h2></li>';
  26.       while ($my_query->have_posts()) : $my_query->the_post(); ?>
  27.         (my post content is here)
  28.        <?php
  29.       endwhile;
  30.     } echo '</ul>';
  31.   }
  32. }
  33. wp_reset_query();  // Restore global post data stomped by the_post().
  34. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement