Title: custom page/post navigation
Last modified: August 19, 2016

---

# custom page/post navigation

 *  Resolved [coby604](https://wordpress.org/support/users/coby604/)
 * (@coby604)
 * [15 years, 11 months ago](https://wordpress.org/support/topic/custom-pagepost-navigation/)
 * Here is the piece of code that I currently have within my loop, that works well
   for navigating forward and backward between posts within a page…
 *     ```
       if ( ! is_search()){ // if returning search results do not show any navigation
       if ( ! is_single()){
       $wp_query->is_single = true;
       $next_post = get_adjacent_post(false,'',false) ;
       $prev_post = get_adjacent_post(false,'',true) ;
   
       if(!get_adjacent_post(false, '', false)) {echo '<div class="post-older post-older-top"><p><a href="#post-'; echo $prev_post->ID; echo '">Older Post ></a></p></div>'; } // if there are no newer articles
   
       elseif(!get_adjacent_post(false, '', true)) {echo '<div class="post-newer post-newer-top"><p><a href="#post-'; echo $next_post->ID; echo '">< Newer Post</a></p></div>'; } // if there are no older articles
   
       else {echo '<div class="post-newer post-newer-top"><p><a href="#post-'; echo $next_post->ID; echo '">< Newer Post</a></p></div>'; echo '<div class="post-older post-older-top"><p><a href="#post-'; echo $prev_post->ID; echo '">Older Post ></a></p></div>'; }
   
       $wp_query->is_single = false;
   
       }else {echo '<div class="post-newer post-newer-top"><p>'; next_post_link('%link','< Newer Post',false); echo '</p></div>'; echo '<div class="post-older post-older-top"><p>'; previous_post_link('%link','Older Post >',false); echo '</p></div>'; }
       }
       ```
   
 * However, when you come to the last post on the page, the link does nothing because
   the next post doesn’t exist on that page it’s on the next page…
 * So what I’m looking for is some sort of code that considers if the post is the
   last post of the page then make the link go to the next page of the blog.
 * Any ideas or help with this is greatly appreciated

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

 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [15 years, 11 months ago](https://wordpress.org/support/topic/custom-pagepost-navigation/#post-1677519)
 * get_adjacent_post
    [http://codex.wordpress.org/Function_Reference/get_adjacent_post](http://codex.wordpress.org/Function_Reference/get_adjacent_post)
 * the idea is already in your code;
    even with comment on the end of the line:
 *     ```
       if(!get_adjacent_post(false, '', false)) {echo '<div class="post-older post-older-top"><p><a href="#post-'; echo $prev_post->ID; echo '">Older Post ></a></p></div>'; } // if there are no newer articles
       ```
   
 * just expand on this.
 *  Thread Starter [coby604](https://wordpress.org/support/users/coby604/)
 * (@coby604)
 * [15 years, 11 months ago](https://wordpress.org/support/topic/custom-pagepost-navigation/#post-1677522)
 * I’m not sure how to expand on this…
 * for example, I have my settings configured to show 5 posts per page. The navigation
   works fine until I reached that fifth post. At that point, the get_adjacent_post
   property will return true because there is another post, but it’s not on the 
   same page therefore the link doesn’t work.
 * So, I’m looking for a solution that figures out when a post is the last on the
   page, and when that happens change the link from:
    `<a href="#post-'; echo $prev_post-
   >ID; echo '">Older Post </a>` to something like this: `<a href="the next page"
   >Next Page </a>`
 * Does that explain my problem better??
 *  Thread Starter [coby604](https://wordpress.org/support/users/coby604/)
 * (@coby604)
 * [15 years, 11 months ago](https://wordpress.org/support/topic/custom-pagepost-navigation/#post-1677528)
 * After some research and trial and error, I found a solution that works for me.
   There might be a simpler, cleaner way of achieving the same thing, but this is
   the code that I came up with that worked…
 *     ```
       if ( ! is_search()){ // if returning search results do not show any navigation
       	if ( ! is_single()){
       		$wp_query->is_single = true;
       		$next_post = get_adjacent_post(false,'',false) ;
       		$prev_post = get_adjacent_post(false,'',true) ;
       		if ($wp_query->current_post == $wp_query->post_count-1 && $wp_query->current_post !== 0) { // you're at the last post on the page
       			if(!get_adjacent_post(false, '', true)) {echo '<div class="post-newer post-newer-top"><p><a href="#post-'; echo $next_post->ID; echo '">< Newer Post</a></p></div>'; } // if there are no older articles
       			else {echo '<div class="post-newer post-newer-top"><p><a href="#post-'; echo $next_post->ID; echo '">< Newer Post</a></p></div>'; echo '<div class="post-older post-older-top"><p>'; $wp_query->is_single = false; next_posts_link('Next Page >', 0); $wp_query->is_single = true; echo '</p></div>'; }
       		}elseif ($wp_query->current_post == 0) { // you're at the first post on the page
       			if(!get_adjacent_post(false, '', false)) {echo '<div class="post-older post-older-top"><p><a href="#post-'; echo $prev_post->ID; echo '">Older Post ></a></p></div>'; } // if there are no newer articles
       			elseif(!get_adjacent_post(false, '', true)) {echo '<div class="post-newer post-newer-top"><p>'; $wp_query->is_single = false; previous_posts_link('< Previous Page', 0); $wp_query->is_single = true; echo '</p></div>'; } // if there are no older articles
       			else {echo '<div class="post-newer post-newer-top"><p>'; $wp_query->is_single = false; previous_posts_link('< Previous Page', 0); $wp_query->is_single = true; echo '</p></div>'; echo '<div class="post-older post-older-top"><p><a href="#post-'; echo $prev_post->ID; echo '">Older Post ></a></p></div>'; }
       		}else { // you're in one of the middle posts
       			if(!get_adjacent_post(false, '', false)) {echo '<div class="post-older post-older-top"><p><a href="#post-'; echo $prev_post->ID; echo '">Older Post ></a></p></div>'; } // if there are no newer articles
       			elseif(!get_adjacent_post(false, '', true)) {echo '<div class="post-newer post-newer-top"><p><a href="#post-'; echo $next_post->ID; echo '">< Newer Post</a></p></div>'; } // if there are no older articles
       			else {echo '<div class="post-newer post-newer-top"><p><a href="#post-'; echo $next_post->ID; echo '">< Newer Post</a></p></div>'; echo '<div class="post-older post-older-top"><p><a href="#post-'; echo $prev_post->ID; echo '">Older Post ></a></p></div>'; }
       		}
       		$wp_query->is_single = false;
       	}
       	else {echo '<div class="post-newer post-newer-top"><p>'; next_post_link('%link','< Newer Post',false); echo '</p></div>'; echo '<div class="post-older post-older-top"><p>'; previous_post_link('%link','Older Post >',false); echo '</p></div>'; }
       }
       ```
   

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

The topic ‘custom page/post navigation’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 3 replies
 * 2 participants
 * Last reply from: [coby604](https://wordpress.org/support/users/coby604/)
 * Last activity: [15 years, 11 months ago](https://wordpress.org/support/topic/custom-pagepost-navigation/#post-1677528)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
