Conditional page/post navigation links in WordPress (redux)

After more helpful input, I've decided to update my original solution.

Instead of overriding four WordPress functions and adding two new ones in my functions.php file, I have slimmed it down to just one:

/**
 * If more than one page exists, return TRUE.
 */
function show_posts_nav() {
	global $wp_query;
	return ($wp_query->max_num_pages > 1);
}

This function will tell me if there is more than one page...and if there is, I will show the posts navigation (next_posts_link and previous_posts_link). I updated my WordPress (index.php, archive.php and search.php) files with:

<?php if (show_posts_nav()) : ?>
<div class='navigation'>
	<span class='older'><?php next_posts_link('&laquo; Older Entries'); ?></span>
	<span class='newer'><?php previous_posts_link('Newer Entries &raquo;'); ?></span>
</div>
<?php endif; ?>

As for the single post next/previous links, I decided to remove the check because I'll always have more than one entry.

Much cleaner and less code ;)

Topics:

41 Comments

  1. January 6, 2008 @ 4:49 pm #

    Hi Eric,
    I come from the JQuery-List to you to read about your fine Modal. And what i see here, this fine solution for a long problem with WP. I'm verry happy, thank you.

  2. January 6, 2008 @ 5:11 pm #

    @Olaf - Thanks, I'm glad it is useful for you. I was surprised that this functionality wasn't built in to WordPress; maybe it will be in a future version.

  3. January 17, 2008 @ 5:52 pm #

    Thanks, I was trying to do this myself just now and noticed they use echo() on those functions so I couldn't test them with empty().

    :)

  4. May 6, 2008 @ 5:53 am #

    Thanks for this idea for sharing!
    Now I have built it into my theme that I'm building for release and it is super!
    No empty divs from now!
    Thanks a lot!
    Andris

  5. May 15, 2008 @ 1:42 pm #

    I'm ready with my theme, if you want take a look at it, maybe it will catch your eye ;)
    styx.kreative-labs.com
    If you like it, use it!
    Again, thanks for the navigation-tip :)

  6. July 3, 2008 @ 12:59 pm #

    I'm not sure how to use this. I tried putting the first code (as is) in my function.php and the second code (as is) in index.php, archives.php and search.php in my theme template files and my pages return with and error at the bottom where the Older Entries link used to be. What am I doing wrong?

  7. July 3, 2008 @ 1:25 pm #

    @Andris - nice template, thanks for sharing!

    @aaron - what error are you getting?

  8. July 5, 2008 @ 10:13 am #

    I just spent an hour looking for a solution to this problem before I found your site. Thanks a lot for posting this. Great work!

  9. July 7, 2008 @ 7:53 am #

    @Leigh - glad to have helped!

  10. July 20, 2008 @ 10:19 am #

    Hey, I am currently migrating from serendipity and I am stumbling upon a couple of things which ar better solved there, because with smarty you get all those things as variables instead of an echo().
    But things like your solution here help a lot!

  11. July 20, 2008 @ 10:39 am #

    btw: "because I’ll always have more than one entry" -> might be different for search results. that's where I needed the code. ;)

  12. July 20, 2008 @ 1:28 pm #

    @mark - One thing I've seen is the ability to pass a boolean that indicates whether you want the value returned or echoed out. I think it would be helpful if WordPress included that type of option for some of its API function.

    To your second comment, I use this in my search.php file as well, just not in single.php. Is that what you were referring to?

  13. August 9, 2008 @ 3:18 pm #

    Ahh, excellent! Thanks so much -- glad to see a proper workaround for a confusing, confusing oversight on the part of WordPress.

  14. October 7, 2008 @ 7:49 pm #

    hi
    i am first time using wordpress
    but i would like to put a page and a post in the home page
    the post is actually a welcome post
    whereas the page is for news
    how can i do that?

  15. October 19, 2008 @ 11:56 am #

    Thanks man! Now that I found your solution I can enjoy the rest of my Sunday.

  16. November 6, 2008 @ 6:18 am #

    Great!
    Thanks for that function.

  17. November 30, 2008 @ 11:55 am #

    Beautiful solution. You just saved me an extra hour of trying to figure out how to make this work. Cheers.

  18. December 22, 2008 @ 10:26 am #

    return ($wp_query->max_num_pages > 1) ? TRUE : FALSE;
    The ternary operator is unnecessary, as the comparison returns a boolean T/F value.

    You can just do this:
    return ($wp_query->max_num_pages > 1);

    and it will return the same thing. Using the ternary operator essentially says "If the statement 'max_num_pages > 1' returns true, then return true". :)

  19. December 22, 2008 @ 10:39 am #

    @Aaron - heh...not sure how I overlooked that, but nice catch ;) Thanks!

  20. December 28, 2008 @ 7:14 pm #

    Just what I needed.. Thanks

  21. January 4, 2009 @ 7:01 am #

    Thanks for the nice code. Just wondering why wordpress is not including this simple function in the script.

  22. January 9, 2009 @ 5:46 pm #

    Correct me if I'm wrong here, but doesn't this just replace the problem of an empty div with an empty span tag (if there is not both a previous and next post)?

    I fail to see how that's much of an improvement. I'm trying to figure out how to do this with list items rather than spans (the semantically correct way), and can't seem to figure it out.

  23. January 9, 2009 @ 6:47 pm #

    @Pankaj - Not sure. All that needs to be done is to put a display parameter, like on some of their other functions, which would allow you to have the value returned as opposed to echo'd.

    @Dean - the point of this "work-around" was to only display the navigation div if it was needed. If you read my original post, you'll see why I wanted to do that, but basically, it allows you to style the div and child elements and not worry about an empty "space" if no other pages/posts existed.

    As to your second point, why is using a list is more semantically correct for one item? Unless, of course, you are talking about building a list of pages, which is not what this post is about. This post is intended to show a function that would allow you to conditionally display the navigation elements, if so desired.

    If you'd like to share what problem you are having (or email me), I'd be happy to try and assist.

  24. January 10, 2009 @ 2:16 pm #

    Hi Eric,

    The semantically correct way to display a group of related links is an unordered list. In most cases, you will have two links - "newer posts" and "older posts". But even when you only have one item, it's still a list. Just as my list of "things to do today" is still a list, even when I've finished all tasks but the last one.

    Someone has already created a plugin to make the page numbers a list:

    http://wordpress.org/extend/plugins/epage-links/

    I would argue that that should probably be an ol, rather than a ul, but close enough :)

    The modification of your code I'm using is:

  25. January 15, 2009 @ 10:58 pm #

    @Dean - I think we're splitting hairs here ;) There are a number of pagination plugins but I'm implementing my own for a new theme I'm working on.

    As you might have notice, the code that you were trying to display did not show up. You need to make sure to escape you HTML entities.

  26. February 11, 2009 @ 10:00 pm #

    Hi Eric,

    I am looking for way to let me navigate from children page to the next children page and if there isn't then it will go to the next category parent. I tried all 3 methods (new and older one beside the one on the forums) but they didn't work for me. Can you help me with this?

  27. March 4, 2009 @ 2:48 pm #

    This is so very helpful. Exactly what I was looking for. Thank you.

  28. March 26, 2009 @ 9:56 am #

    Hmmmm this seems useful, but following up on someone's comment I DO need it for single.php.

    Basically my problem is that I only want to show my link separator if both Next & Previous Links exist (currently an — html equiv.). The posts_nav_link includes a separator as one of the parameters, any idea how to accomplish this for single.php? I want to keep the same links from index.php to single.php -- where you would see "PREVIOUS POST >>" and not "— PREVIOUS POST >>".

  29. March 26, 2009 @ 9:58 am #

    Oh I forgot my example is at http://www.thrivecommunities.com/test/blog !! Will be moved out of the "test" directory when I go live, in case this post lives on. :)

  30. April 7, 2009 @ 2:47 am #

    Will this work for paged comments? If not, what needs to be adapted for it to work?

  31. April 14, 2009 @ 11:57 am #

    Brilliant, exactly what I was looking for. Thanks very much!

  32. April 15, 2009 @ 10:07 pm #

    i was thinking about throwing my computer out a high window... and then i found this...

  33. April 15, 2009 @ 10:48 pm #

    does this code pull "newer posts" nav and "older posts" nav seperately? as in, if i have older posts but not newer, will it only display an older posts nav, etc?

  34. April 21, 2009 @ 9:14 pm #

    @Wisam - sounds like you need to do some custom coding in your functions.php file to handle that.

    @Marcy - were you able to get it working? Checked out your site - you do nice work!

    @CyruzDraxs - sorry, this is just for posts, not comments.

    @fyodor - Haha - glad you didn't throw it out ;) If you look at the second section of code, you'll see where each part (prev, next) are being called.

  35. May 13, 2009 @ 10:39 am #

    What CyruzDraxs said - is there any chance that you would be kind enough to do a version for the fancy new comments system?

  36. May 13, 2009 @ 7:06 pm #

    Hi!
    I had the same problem and didn't where to find the solution.
    Thanks a lot, this is a simple and clean way to fix the problem!

  37. May 18, 2009 @ 11:07 am #

    Hi,
    Good job!
    However, you have hardcoded the max_num_pages to compare with 1.
    At my site learn chinese online, I have used Custom Post Limits plugin from http://coffee2code.com/wp-plugins/custom-post-limits/ where it allows users to define the limits for front page, categories, archives,etc.

    Currently I fixed the limits to a certain value and compare max_num_pages with that value.

    Does anyone know how to get the dynamic limits for each of those different sections?

  38. August 31, 2009 @ 12:35 am #

    Great and simple solution that I am using at almost all my wordpress projects! Thanks mate!

  39. September 4, 2009 @ 1:31 pm #

    Very helpful code. But for some reason, when I click on older entries, none of the posts show up! Help?

  40. September 5, 2009 @ 6:48 am #

    Don't you want to update archive.php not archives.php? Just curious. That might confuse folks too.

  41. September 5, 2009 @ 2:59 pm #

    @Min Min- max_num_pages is calculated using those variables, so you should be fine comparing it it to 1.

    @Tracy - Sounds like a WordPress issue, not something caused by this code. It could be any number of things...

    @Jeff - good catch, thanks! I've updated the post with the correction.

Leave a Comment

Sorry, comments are closed. You can contact me directly, if you like.

Eric Martin
1home
2blog
3projects
4photography
5about
6contact
ssearch
ccomment
p/←previous
n/→next
ttop