This post started as a quick little tip on how to hide navigation blocks when there weren’t any pages to actually link to. That was a good post, it was intelligent, well thought out, and helpful. But like all things intelligent, well thought out, and helpful, it was quickly scrapped.
While coding the new theme for curtishenson.com I had a problem, pagination blocks would show even when there were no pages to link to(not technically wordpress’ fault), turns out it is a pretty easy fix. For post pagination blocks wrap the navigation in this:
<?php global $wp_query;
$total_pages = $wp_query->max_num_pages;
if ( $total_pages > 1 ) {
//Your navigation goes here
}
?>
This grabs the total amount pages, checks if it is greater than one, then shows the navigation if there is more than one page. For comment pagination in WordPress 2.7+ do it like this:
$total_pages = get_comment_pages_count();
if ( $total_pages > 1 ) {
//Your navigation goes here
}
?>
The subtle difference is the use of the get_comment_pages_count function. Otherwise you’re just checking to see if there is more than one page.
A Helper Function
The first time I saw a helper it was in a ruby on rails program. Seemed like a good idea, don’t know why I hadn’t used this most basic implementation of DRY (Don’t Repeat Yourself) theory in WordPress themes. So while I was copy and pasting this code I decided it would be much easier to lump it into one function. The benefits: lighter template files, change the function once and the whole site changes.
This HTML bits will probably need to be changed to your liking, but this little diddy should help anyone with their themes. Just copy and paste the function in your functions.php file, then call it in your theme in place of the navigation like so: <?php ch_navigation_helper(); ?>
I have some ideas to extend this into a truly helpful …helper. I’ll be including it in my new WP Theme Tools Project for sure. If you extend it let me know!
Note: This function only works with WordPress 2.7.1
<?php
// Navigation Helper Function
//http://curtishenson.com/wordpress-navigation-helper-function-comments-and-posts
function ch_navigation_helper(){
global $wp_query;
// For templates like index, search, archive, etc.
if ( !is_singular() ) {
$total_pages = $wp_query->max_num_pages;
if ( $total_pages > 1 ) {
$s = '';
$s .= '<div class="navigation clearfix">';
$s .= '<span class="alignleft">' . get_next_posts_link('Previous Entries') . '</span>';
$s .= '<span class="alignright">' . get_previous_posts_link('Newer Entries') . '</span>';
$s .= '</div>';
echo $s;
}
}
// For paginated comments in WP 2.7+
if ( is_singular() && get_option('page_comments') ) {
$total_pages = get_comment_pages_count();
if ( $total_pages > 1 ) {
$s = '';
$s .= '<div class="navigation clearfix">';
$s .= '<div class="alignleft">';
$s .= get_previous_comments_link('Older Comments');
$s .= '</div>';
$s .= '<div class="alignright">';
$s .= get_next_comments_link('Newer Comments');
$s .= '</div></div>';
echo $s;
}
}
}
?>
4 Comments
Leave a CommentWow! Thank you! I always wanted to write in my blog something like that. Can I take part of your post to my site? Of course, I will add backlink?
10th May 2009
Thank you wery much! I was searching this solution a long time.
5th Nov 2009
Check this version: http://www.kadimi.com/en/wp-paged-navigation-without-plugin-655
I tried to separate language and design from logic, I use that fonction on http://www.amalzone.com, it’s quite satisfying… well, no bugs until now.
18th Aug 2010