WordPress.org

Ready to get started?Download WordPress

Forums

Prev/Next Page plugin? (wordpress Pages, not Posts) (17 posts)

  1. webfox
    Member
    Posted 6 years ago #

    Hello!

    Before I sat down to reinvent the wheel, I thought I'd search around for a plugin that'd enable navigation between wordpress Pages (note the capital P; you know, the so-called "static" things that can have SubPages and exist outside the blog hierarchy) using simple "next" and "previous" links, instead of dumping out the whole hierarchy for navigation. I realize this probably isn't the normal way Pages are expected to be used, but it's rather handy to page through chapters in fiction published using wordpress.

    Searching for anything containing "page" returns... lots of things. I've limited the search then to 'next' and 'previous' but I'm not having much luck.

    Is anyone aware of a plugin that would allow for adding "next" and "previous" links to a Page, basing the sort order (and therefore navigation order) on the "Page Order"? ("menu_order" in the database table)

    This is the last stumbling block preventing me from deleting my movable type installation :)

  2. webfox
    Member
    Posted 6 years ago #

    Didn't find one, so I wrote my own. It's not any sort of quality to post publicly, but it works :P

  3. jvanderkolk
    Member
    Posted 6 years ago #

    webfox, I searched all over the web for the solution for the same problem. Found nothing.

    Could you please post your code? Than I probably will be able to hack it in my Page templates.

    Thanks a lot!

  4. Mindshare Studios, Inc.
    Member
    Posted 6 years ago #

    I wouldn't mind seeing that also... thx!

  5. Kafkaesqui

    Posted 6 years ago #

  6. Earthman Web & Media
    Member
    Posted 6 years ago #

    Kafkaesqui, that code is outdated and incomplete...I modified it and added some extra functionality (yes the code was messy, it's a quick fix)...in any case, here's the updated version for Y'all! (Use the documentation on the link above to insert it)

    <?php
    /*
    Plugin Name: Next-Previous Page
    Version: 0.5
    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) {
    			$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;
    		}
    
    	}
    	}
    }
    ?>
  7. Earthman Web & Media
    Member
    Posted 6 years ago #

    Once you upload that file into your plugins directory and activate it, you can insert using something like:

    <div id="prev_next_links">
    <?php
    $parentid = $post->post_parent;
    previous_page('menu_order', '&lsaquo;&lsaquo; %', '<span class="prev_link">', '</span>', 'title_attr', $parentid); ?>
    <?php next_page('menu_order', '% &rsaquo;&rsaquo;', '<span class="next_link">', '</span>', 'title_attr', $parentid); ?>
    </div>
  8. Earthman Web & Media
    Member
    Posted 6 years ago #

    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;
    		}
    
    	}
    	}
    }
    ?>
  9. gothmog
    Member
    Posted 6 years ago #

    This is EXACTLY what I'm looking for! Thanks so much!

    However, I'm having some problems getting the 'last page' and 'ignore brothers & sisters' stuff to work

    This is the code I'm using to insert into pages:

    <div id="prev_next_links"><div align="left">
    <?php
    $parentid = $post->post_parent;
    previous_page('menu_order', '&lsaquo;&lsaquo; %', '<span class="prev_link">', '</span>', 'Previous Page: %', $parentid); ?>
    </div>
    
    <div align="right">
    <?php next_page('menu_order', '% &rsaquo;&rsaquo;', '<span class="next_link">', '</span>', 'Next Page: %', $parentid); ?>
    </div>
    </div>

    The page in question that has the error is here:
    Page with error

    You can see it at the bottom. Here's a copy of it:

    WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY menu_order LIMIT 0,1' at line 1]
    SELECT ID, post_title FROM wp_posts WHERE menu_order > '' AND post_type = 'page' AND post_status = 'publish' AND ID != 56 AND post_parent = ORDER BY menu_order LIMIT 0,1

    Any ideas how I can fix?
    Thanks!

  10. Barnism
    Member
    Posted 6 years ago #

    I'm also getting a similar error, though only on some pages.

    ALSO - is there any way to set the orderby to page ID?

    I tried hacking around but failed miserably.

    This is almost exactly what I need!

    Thanks.

  11. nachodragon
    Member
    Posted 6 years ago #

    This is 98% of what I need and am attempting to fit it to what I want, but could use a minor tweak. I have gotten it to not show the next parent page or the previous parent page if it is already on a parent page, but not 100%. Easiest way to describe what I want, (and what this almost does) is to navigate the parent and sub-pages beneath but not to touch the other parent pages or pub-pages under another parent page. Any help would be much appreciated.

  12. naa
    Member
    Posted 6 years ago #

    To Kafkaesqui - I tried that plugin but it doesn't work on 2.5.1

    But this is what I am looking for a next/previous page with the option to go by page order or ID. This would be awesome!

  13. naa
    Member
    Posted 6 years ago #

    Ok, just did earthman100's code and good so far! I and some friends of mine write stories so I am using this to go to each chapter and each story, the stories are written in date order so the stories page order has to be consecutive for it to flow properly.

    Was funny when I went in to edit the pages to change their page order this is what wordpress had "Pages are usually ordered alphabetically, but you can put a number above to change the order pages appear in. (We know this is a little janky, it’ll be better in future releases.)"

    ^_^ I hope the improve the PAGE functions in future releases (I'd like more functions for pages), so far everything seems to focus on the POST functions. I've got several layers of links of pages that it can get messy.

    Thanks so much everybody for their hardwork!

  14. hilj
    Member
    Posted 6 years ago #

    This is nice, about 89% of what I was looking for :)

    Few issues:

    1) This is very similar to the problem nachodragon mentions upper. I have parent Pages to kind of group the sub Pages (like category, I don't use the parent Page to display anything), I would like to browse only the sub Pages, so to speak, exclude the parent Page. How could it be done?

    2) I have several of these sub Page sets, and I would like to browse through them all. Now it stops in the end and is not continuing to next set of Pages. Can this be achieved somehow?

    I know this sounds like a job for Posts, but unfortunately, the Posts have some limitations compared to pages, e.g. wp_list_pages, there's no such as wp_list_posts. I could of course use a sitemap-plugin to generate such list of Posts, but non of the sitemap-plugins have any navigation features, such as dynamic menu highlighting, and I'm not skilled to edit them.

    Thanks for this plugin! All help appreciated!

  15. jarussi
    Member
    Posted 6 years ago #

    I'm building a portfolio and want to but all my works, in gallerys, and all teh works are child from the portfolio page..

    The only problem is te first subpage, it has the previous link to the home site!

    anyone having this problem now how to fix it??

    tkz!!

  16. strangelyperfect
    Member
    Posted 6 years ago #

    Hi Webfox
    I don't know if you ever got it sorted. One thing I've started using is TinyMCE Advanced from Andrew Ozz
    It may fulfil your needs. It includes a really easy-to-use page break tag button (a bit like the "more" tag) that sounds like what you want. The "chapters" in your "book" could then be the WP sub-pages structure and you wouldn't need so much hand coding of ID numbers etc.
    It works for "posts" and "pages". The advantage is you can easily page-break where you want so your composition matches the theme layout say. Inserting a line of code in a theme is all it takes to show page 1,2,3 etc and I guess the same for the prev-next structure if that's what you need (although I haven't tried that yet).

  17. niksy
    Member
    Posted 6 years ago #

    Hello,

    I was looking for a way for this, but I didn't find any working solution: Is there a way to navigate only through child pages of the same parent page?

    Thanks in advance and sorry if it's been posted earlier.

    Ivan

Topic Closed

This topic has been closed to new replies.

About this Topic