Conditional page/post navigation links in WordPress (redux)
Monday, November 19th, 2007After 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('« Older Entries'); ?></span>
<span class='newer'><?php previous_posts_link('Newer Entries »'); ?></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
