WordPress Related Posts w/ Thumbs


/ Published in: PHP
Save to your folder(s)

Posts with thumbs by category


Copy this code and paste it in your HTML
  1. <?php $orig_post = $post;
  2. global $post;
  3. $categories = get_the_category($post->ID);
  4. if ($categories) {
  5. $category_ids = array();
  6. foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
  7.  
  8. $args=array(
  9. 'category__in' => $category_ids,
  10. 'post__not_in' => array($post->ID),
  11. 'posts_per_page'=> 3, // Number of related posts that will be shown.
  12. 'caller_get_posts'=>1
  13. );
  14.  
  15. $my_query = new wp_query( $args );
  16. if( $my_query->have_posts() ) {
  17. echo '<h3>Related Posts</h3><ul>';
  18. while( $my_query->have_posts() ) {
  19. $my_query->the_post();?>
  20. <li><a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_post_thumbnail(); ?></a></li>
  21. <?
  22. }
  23. echo '</ul>';
  24. }
  25. }
  26. $post = $orig_post;
  27. wp_reset_query(); ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.