Title: previous_post_link question
Last modified: August 19, 2016

---

# previous_post_link question

 *  [davidswain](https://wordpress.org/support/users/davidswain/)
 * (@davidswain)
 * [17 years ago](https://wordpress.org/support/topic/previous_post_link-question-1/)
 * Hello
 * I have an image gallery made of posts, to have the image clickable to go to the
   previous post ( i post the images in reverse order so image 1 is the last posted,
   image 10 (of ten) the first posted), i have the following code:
 *     ```
       $img = "<img src=\"/image/galleries/" . $catnicename . "/" . $image ."\"  alt=\"" . get_the_title() . "\" />";
   
       		if (previous_post_link('%link', $img, TRUE) == Null) {
       			echo $img;
       		} else {
       			previous_post_link('%link', $img, TRUE);
       		}
       ```
   
 * this does everything i want, on all images excpet the last one clicking on the
   image goes to the next image, whilst on the last one the image is displayed on
   its on.
 * However on all images but the last i have 2 identical images, is there any way
   i can suppress the checking of previous_post_link in the if statment from printing
   to screen?
 * If not can anyone else thing of a way round this problem?
 * Thanks in advance
 * David

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Thread Starter [davidswain](https://wordpress.org/support/users/davidswain/)
 * (@davidswain)
 * [17 years ago](https://wordpress.org/support/topic/previous_post_link-question-1/#post-1062112)
 * Thinking about it could i do another query_posts() with offset within the main
   loop get the permalink?
 *  Thread Starter [davidswain](https://wordpress.org/support/users/davidswain/)
 * (@davidswain)
 * [17 years ago](https://wordpress.org/support/topic/previous_post_link-question-1/#post-1062305)
 * OK
 * I have somewhat of a solution using some code i stole from [eric martin](http://www.ericmmartin.com/conditional-pagepost-navigation-links-in-wordpress/)
   it is:
 *     ```
       $img = "<img src=\"/image/galleries/" . $catnicename . "/" . $image ."\"  alt=\"" . get_the_title() . "\" />";
   
       		/**
       		 * New function that will display the navigation only if a previous or next entry exists
       		 * Hint: For entries, next == newer and previous == older
       		 */
       	function post_navigation() {
       			global $checkthelink;
       			$older = theme_previous_post_link('%link', '� %title');
   
       			if(strlen($older) > 0) {
       				$checkthelink = 1;
       			} 
   
       			return $checkthelink;
       		}
   
       		/**
       		 * Overrides the WordPress previous_posts() function in link-template.php
       		 * Modification: Changed echo to return
       		 */
       		function theme_previous_posts() {
       			return clean_url(get_previous_posts_page_link());
       		}
   
       		/**
       		 * Overrides the WordPress previous_post_link() function in link-template.php
       		 * Modification: Changed echo to return
       		 */
       		function theme_previous_post_link($format='� %link', $link='%title', $in_same_cat = TRUE, $excluded_categories = '') {
   
       				$post = get_previous_post($in_same_cat, $excluded_categories);
   
       			if ( !$post ) {
       				return;
       			} else {
       			$format = 1;
   
       			return $format;
       			}
       		}
   
       		post_navigation();
   
       	if ($checkthelink != 1) {
       		echo $img;
       	} else {
       		previous_post_link('%link', $img, TRUE);
       	}
       ```
   
 * thats as small as i can get it but im sure it could be more efficient, anyone
   can they help?
 * David
 *  [erick_paper](https://wordpress.org/support/users/erick_paper/)
 * (@erick_paper)
 * [16 years, 10 months ago](https://wordpress.org/support/topic/previous_post_link-question-1/#post-1062345)
 * Hi,
 * Did you get any answer to this?
 * I want to show Previous_post_link() and next_post_link() immediately below the
   title, according to my design. But this code yields nothing:
 *     ```
       <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
   
       <div <?php post_class() ?> id="post-<?php the_ID(); ?>">
       <span class="titlenav">
         <span class="prv"><?php previous_post_link('&laquo; %link') ?></span>
         <span class="nxt"><?php next_post_link(' %link &raquo;') ?></span>
       </span>
   
       <a id="content"></a>
       <h2><?php the_title(); ?></h2>
       <span class="date"><?php the_date(); ?> / <a href="<?php trackback_url(); ?>" rel="trackback">Trackback</a>
       </span>
   
       <div class="entry">
       <?php the_content(); ?>
   
       <div class="singlestuff">
       <script type="text/javascript" src="http://w.sharethis.com/button/sharethis.js#publisher=f00c9af2-22dc-4ca6-aea9-c9a255b12f81&type=website"></script>
       <br /><?php wp_link_pages(array('before' => '<strong>Pages:</strong> ', 'after' => '</p><br />', 'next_or_number' => 'number')); ?>
       <?php the_tags( '<span class="tags">Tags: ', ', ', '</span>'); ?>
   
       <div class="navigation">
       <span class="alignleft"><?php previous_post_link('&laquo; %link') ?></span>
       <span class="alignright"><?php next_post_link(' | %link &raquo;') ?></span>
       </div>
   
       </div>
   
       </div>
       </div>
   
       <?php comments_template(); ?>
       <?php endwhile; else: ?>
   
       		<p>Sorry, no posts matched your criteria.</p>
   
       <?php endif; ?>
       ```
   
 * The funny bit is that the second time previous_post_link() is called in the “
   div class=navigation”, it does show up, but it doesn’t show up earlier than the_content().
   Is this a bug? Will previous_post_link only show up after the content?

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘previous_post_link question’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 3 replies
 * 2 participants
 * Last reply from: [erick_paper](https://wordpress.org/support/users/erick_paper/)
 * Last activity: [16 years, 10 months ago](https://wordpress.org/support/topic/previous_post_link-question-1/#post-1062345)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
