Sillybean

Previous and Next links for pages

Next Page options screen

Ever want to let your visitors browse your pages in order? You’ve probably discovered that previous_posts_link and next_posts_link don’t work on pages.

I’ve written a plugin, Next Page, to solve the problem. You’ll get an options screen allowing you to specify the language of your links.

If you’d rather not deal with a plugin, here’s the code you need to add to your page template. I’ve left in a few lines to help with debugging. If your links don’t lead to the pages you expect, you can uncomment those lines to see what’s going on.

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

//print_r($pages);

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

/*
echo "Current page ID's array key: $current";
echo "Previous page ID: $prevID";
echo "Next page ID: $nextID";
*/
?>

<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 also 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>

Posted on July 5, 2009 at 11:27 am in Navigation · comment

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

Leave a Reply

Textile formatting is in effect.