WordPress.org

Ready to get started?Download WordPress

Forums

Randomize related posts (2 posts)

  1. websiter
    Member
    Posted 3 years ago #

    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-->
  2. websiter
    Member
    Posted 3 years ago #

    Sorry about the double post, that was an accident.

Topic Closed

This topic has been closed to new replies.

About this Topic