Advertisement
Guest User

Related Posts

a guest
Apr 9th, 2012
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.32 KB | None | 0 0
  1. <?php $tags = get_the_tags(); ?>
  2.                 <?php if($tags): ?>
  3.                 <?php $related = get_related_posts($post->ID, $tags); ?>
  4.                 <?php if($related->have_posts() && get_option('pyre_related') == 'On'): ?>
  5.                 <?php $count = 1; ?>                           
  6.                         <div class="post-box">
  7. <h4>Related Posts</h4>
  8.                         <?php while($related->have_posts()): $related->the_post(); ?>
  9.                         <?php if($count == 4): $count = 1; endif; if($count == 3): $class = 'last'; else: $class = ''; endif; ?>
  10.                         <?php if(has_post_thumbnail()): ?>
  11.                        
  12.                         <div class="related-item <?php echo $class; ?>">
  13.                             <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?> </a><span class="comments"><?php comments_popup_link('0', '1', '%'); ?></span>
  14.                         <?php endif; ?>
  15.                         <?php $count++; endwhile; ?>
  16.                 </div>
  17. </div>
  18.                 <?php endif; ?>
  19.                 <?php endif; ?>
  20.                 <?php wp_reset_query(); ?>
  21.  
  22. //code in functions.php (defines get_related_posts function) :
  23. function get_related_posts($post_id, $tags = array()) {
  24.     $query = new WP_Query();
  25.     if($tags) {
  26.         foreach($tags as $tag) {
  27.             $tagsA[] = $tag->term_id;
  28.         }
  29.     }
  30.     $args = wp_parse_args($args, array(
  31.         'showposts' => 3,
  32.         'post__not_in' => array($post_id),
  33.         'tag__in' => $tagsA,
  34.         'ignore_sticky_posts' => 1,
  35.     ));
  36.     $query = new WP_Query($args);
  37.     return $query;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement