• I have some code in place that gets the tags of the current posts and displays a list of related posts with the associated thumbnail. The problem is that the many of the same posts are showing up again and again on different posts because its only displaying the top 5 results returned. I need to randomize it a little so that the related posts are more varied, but still related by tag. Here is the code:

    <!--BEGIN STORIES YOU'LL LOVE-->
    <?php
    				$tags = wp_get_post_tags($post->ID);
    				if ($tags) {
    					$tag_ids = array();
    					foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
    
    					$args=array(
    						'tag__in' => $tag_ids,
    						'post__not_in' => array($post->ID),
    						'showposts'=>5, // Number of related posts that will be shown.
    						'ignore_sticky_posts'=>1
    					);
    					$my_query = new wp_query($args);
    
    					if( $my_query->have_posts() ) {
    						echo '<div class="related-posts"><h2>Stories You\'ll Love</h2>';
    						while ($my_query->have_posts()) {
    							$my_query->the_post();
    						?>
    							<div class="related-post">
    
                                <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
    							<?php show_thumb($width=110,$height=110,$crop='T',$quality=95,$blank=false); ?>
    							<?php the_title(); ?></a></div>
    						<?php
    						}
    						echo '<div style="clear:both"></div></div>';
    					}
    				};
    				wp_reset_query();
    ?><!--END STORIES YOU'LL LOVE-->

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Randomize related posts’ is closed to new replies.