Advertisement
alchymyth

cat loops

Apr 4th, 2012
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php  $cats = array('film-reviews','poetry'); //an array with category slugs;
  2.  
  3. foreach ($cats as $cat_slug) :
  4. $cat = get_term_by('slug',$cat_slug,'category');
  5. $cat_id = $cat->term_id;
  6.  
  7. $args = array(
  8. 'posts_per_page' => 3, // max number of post per category
  9. 'category__in' => array($cat_id)
  10. );
  11. $cat_query = new WP_Query($args);
  12.  
  13.      if ($cat_query->have_posts()) :
  14.      
  15.      echo '<h2>'.$cat->name.'</h2>';
  16.      
  17.      while ($cat_query->have_posts()) : $cat_query->the_post(); ?>     
  18.  
  19.     <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>              
  20.                
  21.     <?php the_excerpt(); ?>
  22.    
  23.     <?php endwhile; ?>
  24.        
  25.     <?php else :
  26.         echo '<h2>No Posts for '.$cat->name.'</h2>';               
  27.      endif;
  28.      
  29.      wp_reset_query(); ?>
  30.    
  31. <?php endforeach; ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement