oops, missed one detail, here is a new version:
<?php
/*
Plugin Name: Next-Previous Page
Version: 0.6
Plugin URI: http://wordpress.org/support/topic/43071
Description: Next and Previous Page navigation; by post date or Page (menu) order.
Author: Kaf Oseo, Andy Peatling, Terran Orletsky
Author URI: http://szub.net
Copyright (c) 2005, Kaf Oseo (http://szub.net)
Next-Previous Page is released under the GNU General Public
License: http://www.gnu.org/licenses/gpl.txt
This is a WordPress plugin (http://wordpress.org).
**
* September 9th 2006: Plugin modified by Andy Peatling (http://cssdev.com)
* -- Added support for limiting to brother and sister pages only.
**
**
* December 18th 2007: Plugin modified by Terran Orletsky (http://earthman.ca)
* -- Added support for WP 2.3.1 and added auto-detection of Child and Parent hierarchical pages only.
**
*/
function next_page($orderby = 'post_date', $link='Next Page: %', $before='', $after='', $title_attr='Next Page: %', $parent_id = 0) {
global $wpdb, $wp_query;
if(is_page()) {
$post = $wp_query->post;
if(('page_order' == $orderby) || ('menu_order' == $orderby)) {
$orderby = 'menu_order';
$typeselect = "menu_order > '$post->menu_order'";
} else {
$orderby = 'post_date';
$typeselect = "post_date > '$post->post_date'";
}
//check to see if there are children, if so include them
$nextchildpage = @$wpdb->get_row("SELECT ID, post_title FROM $wpdb->posts WHERE post_type = 'page' AND post_status = 'publish' AND ID != $post->ID AND post_parent = $post->ID ORDER BY $orderby");
if ($nextchildpage) {
$title = apply_filters('the_title', $nextchildpage->post_title);
$link = str_replace('%', $title, $link);
$title_attr = str_replace('%', $title, $title_attr);
echo $before.'<a href="'.get_permalink($nextchildpage->ID).'"';
if($title_attr)
echo ' title="'.$title_attr.'"';
echo '>'.$link.'</a>'.$after;
}
else //no kids
{
$nextpage = @$wpdb->get_row("SELECT ID, post_title FROM $wpdb->posts WHERE $typeselect AND post_type = 'page' AND post_status = 'publish' AND ID != $post->ID AND post_parent = " . $parent_id . " ORDER BY $orderby LIMIT 0,1");
if ($nextpage) {
$title = apply_filters('the_title', $nextpage->post_title);
$link = str_replace('%', $title, $link);
$title_attr = str_replace('%', $title, $title_attr);
echo $before.'<a href="'.get_permalink($nextpage->ID).'"';
if($title_attr)
echo ' title="'.$title_attr.'"';
echo '>'.$link.'</a>'.$after;
}
else
//check to see if there is a next page on the parent level to move to
{
$parents_parent = @$wpdb->get_row("SELECT post_parent, menu_order FROM $wpdb->posts WHERE ID = " . $parent_id );
$nextparentpage = @$wpdb->get_row("SELECT ID, post_title FROM $wpdb->posts WHERE menu_order > '".$parents_parent->menu_order."' AND post_type = 'page' AND post_status = 'publish' AND ID != $post->ID AND post_parent = " . $parents_parent->post_parent . " ORDER BY $orderby LIMIT 0,1");
if ($nextparentpage) {
$title = apply_filters('the_title', $nextparentpage->post_title);
$link = str_replace('%', $title, $link);
$title_attr = str_replace('%', $title, $title_attr);
echo $before.'<a href="'.get_permalink($nextparentpage->ID).'"';
if($title_attr)
echo ' title="'.$title_attr.'"';
echo '>'.$link.'</a>'.$after;
}
}
}
}
}
function previous_page($orderby = 'post_date', $link='Previous Page: %', $before='', $after='', $title_attr='Previous Page: %', $parent_id = 0) {
global $wpdb, $wp_query;
if(is_page()) {
$post = $wp_query->post;
if(('page_order' == $orderby) || ('menu_order' == $orderby)) {
$orderby = 'menu_order';
$typeselect = "menu_order < '$post->menu_order'";
} else {
$orderby = 'post_date';
$typeselect = "post_date < '$post->post_date'";
}
$lastpage = @$wpdb->get_row("SELECT ID, post_title FROM $wpdb->posts WHERE $typeselect AND post_type = 'page' AND post_status = 'publish' AND ID != $post->ID AND post_parent = " . $parent_id . " ORDER BY $orderby DESC LIMIT 0,1");
if ($lastpage) {
//FIRST check to see if there are children, if so include them
$prevchildpage = @$wpdb->get_row("SELECT ID, post_title FROM $wpdb->posts WHERE post_type = 'page' AND post_status = 'publish' AND ID != $post->ID AND post_parent = $lastpage->ID ORDER BY $orderby DESC");
if ($prevchildpage) {
//echo "SELECT ID, post_title FROM $wpdb->posts WHERE post_type = 'page' AND post_status = 'publish' AND ID != $post->ID AND post_parent = $parentpage->ID ORDER BY $orderby DESC";
$title = apply_filters('the_title', $prevchildpage->post_title);
$link = str_replace('%', $title, $link);
$title_attr = str_replace('%', $title, $title_attr);
echo $before.'<a href="'.get_permalink($prevchildpage->ID).'"';
if($title_attr)
echo ' title="'.$title_attr.'"';
echo '>'.$link.'</a>'.$after;
} else //no children, show the prev parent level page
{
$title = apply_filters('the_title', $lastpage->post_title);
$link = str_replace('%', $title, $link);
$title_attr = str_replace('%', $title, $title_attr);
echo $before.'<a href="'.get_permalink($lastpage->ID).'"';
if($title_attr)
echo ' title="'.$title_attr.'"';
echo '>'.$link.'</a>'.$after;
}
}
else
//show the parent link back
{
$parentpage = @$wpdb->get_row("SELECT ID, post_title FROM $wpdb->posts WHERE post_type = 'page' AND post_status = 'publish' AND ID = " . $parent_id);
if ($parentpage) {
$title = apply_filters('the_title', $parentpage->post_title);
$link = str_replace('%', $title, $link);
$title_attr = str_replace('%', $title, $title_attr);
echo $before.'<a href="'.get_permalink($parentpage->ID).'"';
if($title_attr)
echo ' title="'.$title_attr.'"';
echo '>'.$link.'</a>'.$after;
}
}
}
}
?>