Advertisement
Guest User

Same category

a guest
Sep 1st, 2011
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.87 KB | None | 0 0
  1. <?php
  2.     // put all the category IDs of current post into an array
  3.     $categories = get_the_category( $post->ID );
  4.     $category_id_array = array();
  5.     foreach ( $categories as $category ) {
  6.         $category_id_array[] = $category->cat_ID;
  7.     }
  8.    
  9.     // define how many thumbnails to show (at most)
  10.     $thumbnail_max = 6;
  11.    
  12.     // grab all posts in the same categories as the current post
  13.     $category_thumb_args = array( 'category__in' => $category_id_array, 'posts_per_page' => $thumbnail_max, 'exclude' => $post->ID );
  14.     $category_thumb_query = new WP_Query( $category_thumb_args );
  15.    
  16.     // if there are posts loop through them
  17.     if ( $category_thumb_query->have_posts() ) :
  18.         while($category_thumb_query->have_posts()) : $category_thumb_query->the_post(); ?>
  19.            
  20.  
  21.             ---- my stuff ---
  22.    
  23.         <?php endif;
  24.         endwhile;
  25.     endif;
  26.     // reset the query since we used the_post()
  27.     wp_reset_query();
  28.     ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement