Advertisement
ssaidz

Related Posts Tag WordPress

Sep 21st, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.79 KB | None | 0 0
  1. <div class="relatedposts">
  2. <h3>Related posts</h3>
  3. <?php
  4.   $orig_post = $post;
  5.   global $post;
  6.   $tags = wp_get_post_tags($post->ID);
  7.    
  8.   if ($tags) {
  9.   $tag_ids = array();
  10.   foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
  11.   $args=array(
  12.   'tag__in' => $tag_ids,
  13.   'post__not_in' => array($post->ID),
  14.   'posts_per_page'=>4, // Number of related posts to display.
  15.   'caller_get_posts'=>1
  16.   );
  17.    
  18.   $my_query = new wp_query( $args );
  19.  
  20.   while( $my_query->have_posts() ) {
  21.   $my_query->the_post();
  22.   ?>
  23.    
  24.   <div class="relatedthumb">
  25.     <a rel="external" href="<? the_permalink()?>"><?php the_post_thumbnail(array(150,100)); ?><br />
  26.     <?php the_title(); ?>
  27.     </a>
  28.   </div>
  29.    
  30.   <? }
  31.   }
  32.   $post = $orig_post;
  33.   wp_reset_query();
  34.   ?>
  35. </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement