get_adjacent_post
http://codex.ww.wp.xz.cn/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.
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??
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>'; }
}