Sillybean

Next Page

This plugin provides shortcodes and template tags for next/previous navigation in pages.

Download it at wordpress.org »

If you’d rather not deal with the overhead of a plugin, here’s the condensed code for your page template file:

<?php
$pagelist = get_pages('sort_column=menu_order&sort_order=asc');
$pages = array();
foreach ($pagelist as $apage) {
   $thepages[] += $apage->ID;
}

$current = array_search($post->ID, $thepages);
$prevID = $thepages[$current-1];
$nextID = $thepages[$current+1];
?>

<div class="navigation">
<?php if (!empty($prevID)) { ?>
<div class="alignleft">
<a href="<?php echo get_permalink($prevID); ?>"
  title="<?php echo get_the_title($prevID); ?>">Previous</a>
</div>
<?php }
if (!empty($nextID)) { ?>
<div class="alignright">
<a href="<?php echo get_permalink($nextID); ?>" 
 title="<?php echo get_the_title($nextID); ?>">Next</a>
</div>
<?php } ?>
</div><!-- .navigation -->

You can emulate Drupal’s book feature and include a link to the page parent as well:

<div class="aligncenter">
<a href="<?php echo get_permalink($post->post_parent); ?>" 
 title="<?php echo get_the_title($post->post_parent); ?>">Up one level</a>
</div>

Version 1.2 adds an option to exclude pages by ID. You can simply add the exclude parameter to the get_pages() function in your template code:

$pagelist = get_pages('sort_column=menu_order&sort_order=asc&exclude=6,21,47');
70 comments

Sharing this post? The short URL is http://sillybean.net/?p=2275

70 Responses to “Next Page”

  1. Ed says:

    Sorry. Cannot help you with that. Have not encountered that problem so far. Could be a browser issue, maybe?

  2. Tracy says:

    It’s not a browser issue that I can see. I’ve tried it on Firefox, Safari and Opera so far and all three have the same problem.

    Thanks for trying to help.

  3. Stephanie says:

    It is not a browser issue. I think I’ve identified the problem, but I’ve been swamped at work today and haven’t had time to test it. Tracy, try changing the $page variable to something else. Apparently it’s a global in WP, which I’d never noticed, and I’m pretty sure that’s the problem.

  4. Tracy says:

    That’s funny, Stephanie, I was just identifying that as the problem! Yeah, it’s used extensively in the post-template.php file.

    I changed both the $pages and $page variables and it worked.

    Thanks for spending the time to troubleshoot!

  5. Ed says:

    @Tracy what did you change them to? Will be nice to know for future reference in WP.

  6. Tracy says:

    @Ed I just changed them to $projectpage and $projectpages respectively because those names mean something to my specific pages. You can change them to whatever you want to though. Just make sure to change the name of all instances of each variable. (I didn’t change the $pagelist variable.)

  7. Stephanie says:

    I’ve just updated this page with better variables. Thanks for pointing out the problem, Tracy!

  8. Ed says:

    Thanks @Stephanie and @Tracy for all the help.

  9. Tracy says:

    Glad to have a problem that could help everyone! Thanks @Stephanie for your help.

  10. Ed says:

    Thank you for sharing it with us. This way we all get to learn new things.

  11. Simon says:

    it was[sic] nice to come to a dead stop at the first chapter or the last, instead of leaking over into the next book’s hierarchy.

    I’ve made 2 changes to this plugin:

    I too ran into the same issue as what Derek states an I think (imho) this in an improvement:

    Slightly changed Code

    The other thing is that I’ve removed the echos from the template tags. Lines 244,248,252 as I want to move the navigation outside the loop. You can put them back in if you wish.

    Hope this helps someone. Excellent plugin!

  12. Ed says:

    Thanks a lot Steph. Just upgraded it today. Will let you know if I run into any problems.

  13. Ed says:

    Are you using the script in your HTML to activate it?

Leave a Reply

Textile formatting is in effect.