Conditional page/post navigation links in WordPress (redux)

November 19th, 2007

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) ? TRUE : FALSE;
}

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, archives.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 ;)

Bookmark:
  • Digg
  • del.icio.us
  • Technorati
  • StumbleUpon
  • Reddit
  • Ma.gnolia
  • Facebook
  • Furl
  • Fark
  • Google
  • Slashdot

Tags: , ,

13 Responses to “Conditional page/post navigation links in WordPress (redux)”

  1. Olaf

    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. Author Comment

    Eric Martin

    @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. jive

    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. Andris

    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. Andris

    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. aaron

    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. Author Comment

    Eric Martin

    @Andris - nice template, thanks for sharing!

    @aaron - what error are you getting?

  8. Leigh

    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. Author Comment

    Eric Martin

    @Leigh - glad to have helped!

  10. mark

    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. mark

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

  12. Author Comment

    Eric Martin

    @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. Joseph X. Burke

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

Leave a Reply

If you'd like to post code in your comment, please wrap your code with a pre and code tag. For example, <pre><code>CODE</code></pre>. Additionally, you'll need to escape the HTML entities (try Postable), otherwise the code will not display properly.