Advertisement
alchymyth

category sections

Feb 19th, 2012
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.16 KB | None | 0 0
  1. <?php  $cats = array('News Updates', 'Latest Updated Products'); //an array with the category names
  2.  
  3. foreach ($cats as $cat) :
  4.  
  5. $cat_id = get_cat_ID( $cat ); // http://codex.wordpress.org/Function_Reference/get_cat_ID
  6.  
  7. if( $cat_id ) : //only do the next if a the category name reuslted in a cat_id
  8.  
  9. echo '<div class="cat-section">';
  10.  
  11. $args = array(
  12. 'posts_per_page' => 3, // max number of post per category
  13. 'category__in' => array($cat_id) //only posts with directly this cat_id
  14. );
  15. $query = new WP_Query($args);
  16.  
  17.      if ($query->have_posts()) :
  18.      
  19.      echo '<h2 class="cat-title">' . $cat . '</h2>';
  20.      
  21.      while ($query->have_posts()) : $query->the_post(); ?>     
  22.  
  23.             <div <?php post_class() ?>>
  24.                 <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>             
  25.  
  26.                 <div class="entry">
  27.                 <?php the_excerpt(); ?>
  28.                 </div> 
  29.             </div>
  30.  
  31.         <?php endwhile; ?>
  32.        
  33.     <?php else :
  34.         echo '<h2>No Posts for '.$cat.'</h2>';             
  35.      endif;
  36.      
  37.      wp_reset_postdata(); ?>
  38.    
  39. <?php
  40. echo '</div> <!--end of .cat-section-->';
  41.  
  42. endif; // ENDS if( $cat_id ) :
  43.  
  44. endforeach; ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement