Title: Problem with Permalink and Loop
Last modified: August 19, 2016

---

# Problem with Permalink and Loop

 *  [mcfadden](https://wordpress.org/support/users/mcfadden/)
 * (@mcfadden)
 * [18 years, 1 month ago](https://wordpress.org/support/topic/problem-with-permalink-and-loop/)
 * Hello, I have set up a loop here to display 3 random posts for a certain category.
   Everything works great, except the permalink does not go to the post and instead
   just refreshes the category page. Help me please. You can see this code in action
   at [http://fusionhealthnews.com/category/healthy-living/](http://fusionhealthnews.com/category/healthy-living/).
   It is the second box of three posts with the apples next to them.
 * Thank you many times in advance. My code is below. Sorry it is so sloppy.
 *     ```
       <?php
       global $wpdb;
       $numposts = 3;
   
       $rand_posts = $wpdb->get_results("
       SELECT DISTINCT ID, post_title
       FROM $wpdb->posts as p
       INNER JOIN $wpdb->term_relationships AS tr ON
       (p.ID = tr.object_id AND
       tr.term_taxonomy_id IN (18) )
       INNER JOIN $wpdb->term_taxonomy AS tt ON
       (tr.term_taxonomy_id = tt.term_taxonomy_id AND
       taxonomy = 'category')
       WHERE post_status = 'publish'
       ORDER BY RAND()
       LIMIT $numposts");
   
       foreach($rand_posts as $post) :
       setup_postdata($post);
   
       // If the feature is found in the query, skip over it
       // Subtract "1" from $i to compensate for the lost post
       	if ($post->ID == $do_not_duplicate) {
       		continue;
       		update_post_caches($posts);
       		$i--;
       		}
   
         $thumb = '/wp-content/themes/fusion/images/healthyliving.jpg';
         $thumb_alt = 'Healthy Living';
       // Checks to see if there's a custom "short" excerpt for front page display
       	$excerpt = get_post_custom_values($key = 'Excerpt');
       	?>
   
       <div id="recent-post-<?php the_ID(); ?>" class="post">
   
       <?php
       	// Checks thumbnail array to see if there's any information in the Value field
       	if(isset($thumb[0]) && strcmp($thumb[0],'')!= 0)  {
       		?>
       		<img src="<?php echo $thumb; ?>" alt="<?php if($thumb_alt !== '') echo $thumb_alt; else echo the_title(); ?>" class="left" />
       	<?php } // endif ?>
   
       	<div class="entry">
       	<h3 class="post-title"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
       	<?php
       	// Checks excerpt array to see if there's any information in the Value field
       		if(isset($excerpt[0]) && strcmp($excerpt[0],'')!= 0) echo $excerpt[0];
       	// If no Value set, display normal excerpt
       		else the_excerpt();
       	?>
       	</div><!-- entry -->
       </div><!-- post -->
       <?php
       	$i++; // Iterate the $i variable
       	endforeach; // endwhile - loop
       ?>
       ```
   

The topic ‘Problem with Permalink and Loop’ is closed to new replies.

## Tags

 * [loop](https://wordpress.org/support/topic-tag/loop/)
 * [permalink](https://wordpress.org/support/topic-tag/permalink/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 0 replies
 * 1 participant
 * Last reply from: [mcfadden](https://wordpress.org/support/users/mcfadden/)
 * Last activity: [18 years, 1 month ago](https://wordpress.org/support/topic/problem-with-permalink-and-loop/)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
