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

Comments

  1. Hey there! This plugin is just what i needed but I installed it and I’m only getting arrows on my posts rather than Previous: Title or Next: Title.

    Is it my theme or must I change some settings?

    Thanks for your time and the great plugin!

    Posted by TheBeeOBee on July 10th, 2009 at 7:48 pm

  2. There is a settings page — it should be under options. If you have entered something there and it’s not showing up, let me know!

    Posted by Stephanie on July 10th, 2009 at 9:12 pm

  3. Hi Stephanie,

    http://wordpress.org/extend/plugins/next-page/screenshots/

    I left them at the default which looks exactly like these settings. Should that default be producing only an arrow?

    Sorry for the newb-ish questions. My friend who does the coding for me is not around and won’t be for about a month and I’d like to try to work this out.

    Posted by TheBeeOBee on July 10th, 2009 at 11:25 pm

  4. Sometimes it’s really that simple, isn’t it? I feel a little stupid for not thinking of this myself/earlier, though.

    Posted by Aion Vor on July 28th, 2009 at 2:22 pm

  5. TheBeeOBee — sorry I didn’t get back to you sooner; I was on vacation when your comment came in and I overlooked it in my inbox! You should definitely get more than arrows. Can I take a look at the pages in question?

    Posted by Stephanie on July 28th, 2009 at 2:39 pm

  6. More for TheBeeOBee — are you using the shortcode or the template tag? I’ve just realized that there’s a typo in the template tag shown on the options screen; it should be <?php instead of <php. I’m fixing that in the code now.

    Posted by Stephanie on July 28th, 2009 at 2:50 pm

  7. I thought I would have the ability to apply this plug-in to specific set of pages. However, I can’t find any way to be able to this especially in the Atahualpa theme.

    Posted by Jeff K on August 9th, 2009 at 5:23 pm

  8. Jeff, you could use the short tags on the specific pages you want. Otherwise, the template tags would need to go in your theme’s page file, and if you wanted it to show only on a few pages, you’d need to set up some PHP to test the page IDs or something.

    Posted by Stephanie on August 9th, 2009 at 7:28 pm

  9. Is there a way to get the arrows to show? I only see the text. Is there a way to ONLY get the arrows to show?

    Please let me know. Thank you so much for your help! Talk soon.

    Posted by Logan on August 25th, 2009 at 10:08 am

  10. Hi Stephanie,
    Thanks so much for the good work. I’ve gone for the code in the page template option, so easy, it’s brilliant!

    Just wondering if I can check a problem with you though…

    I am using another plugin called “page links to” which redirects each main page to the first child page in it’s section. The next buttons are working perfectly because once it hits a redirected page it is redirected, but when I use the previous button it gets stuck on the redirected page. Is there a way I can ask it to skip a page when it comes to that problem. Sorry if it sounds confusing.

    Thanks again,
    Belinda

    Posted by Belinda on September 17th, 2009 at 7:47 am

  11. Belinda — interesting scenario! Would it help to have a box on the options page where you could enter a list of page IDs to be excluded?

    Posted by Stephanie on September 17th, 2009 at 2:56 pm

  12. Yes, that would be fabulous!

    Posted by Belinda on September 17th, 2009 at 4:54 pm

  13. Ok, that’s easy. I think I have it working. I’ll release an update later tonight.

    Posted by Stephanie on September 17th, 2009 at 5:44 pm

  14. WOW. Thanks so much!!

    Posted by Belinda on September 17th, 2009 at 5:57 pm

  15. Is there a way to get both arrows AND text to show? I can only get the text to show using the shortcut codes. Thanks!

    Posted by Logan on September 17th, 2009 at 6:34 pm

  16. Logan, are you by chance using the Front-end Editor plugin? I’ve noticed that the two conflict for some reason, and when I disable the editor, my arrows show up again. Let me know if that’s not the case for you! Also try version 1.2, which I’ve just uploaded to the plugin repository, and see if that works better for you.

    Belinda, 1.2 includes the page exclusion option. Enjoy!

    Posted by Stephanie on September 17th, 2009 at 10:08 pm

  17. I don’t have that Front-end Editor Plugin. At least, not that I see. I will try version 1.2 and see what happens. Thank you so much for your help!

    Posted by Logan on September 18th, 2009 at 9:04 am

  18. hi,

    i am using your plug-in, but the previous and next buttons link to the post that i am already on. they don’t go to the previous or next post.
    do you know how i can fix this?

    thank you so much!

    Posted by big will on October 4th, 2009 at 3:41 pm

  19. Is it possible to have the Previous and Next Page links at the bottom of the page rather than on the top? Maybe even have it centered on its own line?
    Thank you.

    Posted by Ed on October 19th, 2009 at 6:38 am

  20. Hello,

    This plugin is working quite nicely for a project of mine. If you have time, I was wondering if you could help me find a way to disable or (preferably) remove/hide the next link when the user comes to the last page in the list. What do you think? I can edit the plugin directly if you can point me in the right direction. Thank you for a great plugin!

    Posted by Wee on October 26th, 2009 at 11:38 pm

  21. Hi again :)

    I just realized that I could easily do that using CSS (I heart dynamic body classes). Sorry!! But seriously, this is a very nice, easy-to-implement plugin. You rock.

    Posted by Wee on October 26th, 2009 at 11:47 pm

  22. It’s possible that I’m simply not doing something correctly, but when I activate the plugin, I still don’t see the Previous and Next links on my posts. I’m still very much a noob at WordPress, so maybe there’s something I’m missing on how to implement this plugin. Is there a step-by-step tutorial on exactly what steps to follow in order to get the previous/next links to show up on individual posts?

    Posted by Stinky on November 12th, 2009 at 8:18 pm

  23. Stinky: You need to copy the PHP code given on the options screen and paste it somewhere in your theme files. If you have a single.php file, it goes in there.

    Ed, I’m sorry I overlooked your comment. If the links are appearing at the top of the page, you’ve either pasted them there yourself (in which case you can just move them), or you’re confusing these links with WP’s built-in previous/next links for posts, which appear at the top of the page in a lot of themes.

    Posted by Stephanie on November 12th, 2009 at 8:22 pm

  24. Hi, Stephanie — sorry for sounding thick about this, but where’s the options screen? I’m using the Heatmap for Adsense theme, and it has an options page… but I don’t see anything pertaining to the Next Page plugin there. Am I just missing it somehow?

    Also, does it matter where I paste the php code in the single.php file?

    Thanks for your help!

    Posted by Stinky on November 12th, 2009 at 10:02 pm

  25. Look under Settings -> Next Page. And yes, it matters: where you paste the code determines where the links appear. I’m not familiar with your theme, but look for a line like < ?php endforeach; ?> or < ?php endwhile; ?> and paste the code just after that. Just experiment until you get the links placed where you want them!

    Posted by Stephanie on November 12th, 2009 at 10:13 pm

  26. Thanks very much, Stephanie. I’ve got the links where I want them (mostly — I can keep tweaking later). Now, though, I’m having the same trouble as big will — namely, the links are pointing to the page that the user is already on, instead of the next/previous pages.

    Any idea what might be causing this?

    Posted by Stinky on November 12th, 2009 at 10:31 pm

  27. Just to clarify, here’s the code I’m using. I’ve got it set directly below the title area, and I set the div alignment within the page, rather than aligning via class in the options page. I’m not using any short code (I’m not sure how to use that, or if I’m supposed to).

    |

    Am I missing something?

    Posted by Stinky on November 12th, 2009 at 10:47 pm

  28. Oops. Code doesn’t show up in the post. Let’s try again.

    ‹div align=”right”›‹?php previous_link(); ?› | ‹?php next_link(); ?›‹/div›

    Posted by Stinky on November 12th, 2009 at 10:49 pm

  29. I had the same problem too. Nothing displays on my page. Not sure if the default next page code might be stopping it from showing somehow.

    Posted by Ed on November 13th, 2009 at 11:54 am

  30. You guys, I’m really sorry: I told you the wrong file to put the code in. It should be page.php, not single.php. That’s undoubtedly why the links are not showing up and/or pointing to the current page. I apologize; I’ve been on some weird medications for the last week or so. :(

    Posted by Stephanie on November 13th, 2009 at 1:46 pm

  31. Hi, Stephanie. Thanks for getting back to me. Unfortunately, when I put the code on my page.php file, it doesn’t appear to be showing up at all now. I’ve tried both the code you provide up top, as well as the direct code listed at the top of the page.

    Because I edited the single.php page, could that be messing with the page.php file and preventing the next/previous navigation from showing up? It does look as though changes to the single.php page are overriding any changes to the page.php file.

    Any thoughts as to why this might not be working? From what I’ve gathered, this should be a fairly simple copy/paste exercise — at least in terms of getting the plugin to function. So I’m at a loss as to what I’m doing incorrectly.

    Posted by Stinky on November 13th, 2009 at 10:17 pm

  32. I am, too! Single.php doesn’t affect pages at all (see the WP template hierarchy). All I can suggest is that you try moving the code to different points in your page.php file and see what happens.

    Posted by Stephanie on November 13th, 2009 at 10:22 pm

  33. Hey Stephanie. Could the default in WP be interfering with your plugin. Just does not want to work for me yet. How do I deactivate the default nextpage? I liked Chan’s plugin too but could not get it to work.

    Posted by Ed on November 16th, 2009 at 3:12 pm

  34. I have one question, your links go by the alphabet, I have set my pages up with numbers, how do I change the code so the previous and next pages will go to the ones I want according to their number, not their alphabet.

    Posted by John Cardell on November 18th, 2009 at 5:24 pm

  35. @John, I thought that the pages go by the numbers assigned by WP to them in order.

    Posted by Ed on November 19th, 2009 at 10:51 am

  36. I’ve been using the plugin variety of the above, and it’s great – the one nitpick I had was that when I’m navigating subpages, I don’t want the last page to jump to the next parent page in the hierarchy. So, I fixed that by adding ‘&child_of’ to the flatten_page_list() call and passing in the $post->parent_page.

    Then, instead, the plugin was showing a link to the current page when it was the last one/first one in the list (thereby indicating that nothing followed/preceded it). Fixing that required:

    @ if ($current+1 >= count($pages)) return false; else ….. @

    and

    @ if ($current-1 < 0) return false; else ….. @

    in next_page and prev_page, respectively. I'm not sure if this is the kind of thing anyone else would actually need or want, but for flipping chapters in an online 'book' it was nice to come to a dead stop at the first chapter or the last, instead of leaking over into the next book's hierarchy.

    Posted by Derek on November 21st, 2009 at 12:21 pm

  37. @Derek can you post your code please? I cannot get it to work for me.
    Thank you.

    Posted by Ed on November 24th, 2009 at 10:26 am

  38. Awesome plugin!
    When I put the exclude page numbers in, it doesn’t seem to remove them (from faq, contact etc pages). You can see it here http://onlineguitarcoaching.com/faq/

    Thanks!

    Posted by Will on November 27th, 2009 at 11:54 am

  39. @Will I could not see what you are referring to above.

    Posted by Ed on November 30th, 2009 at 10:49 am

  40. Ed – if you go to the URL above, the next/previous links are on faq, about, and other pages. I don’t want that. Just want them on my courses page (so students can go thru the lessons one at a time). so I put exclude ids but those pages aren’t being excluded.

    Posted by Will on November 30th, 2009 at 1:55 pm

  41. @will I see what you mean. I am wondering if the default next page code could be doing it as it does not let me display this code either.

    Posted by Ed on December 1st, 2009 at 1:01 pm

  42. Hello,

    I am using your plugin and it works except for when I use exclude posts. When I do exclude posts it puts the name and link of the current page for both the next and previous links.

    The install I am working off of is Wordpress 2.86 – not sure if you were aware of the bug or not. For my project, it would be ideal if I could select a post parent and have the plugin cycle endlessly through the children. Excluding posts would be ok if it worked.

    Hope you get a fix out there. Thanks.

    Posted by George on December 8th, 2009 at 8:19 pm

  43. I am still having issues with it with the latest WP I am using.

    Posted by Ed on December 14th, 2009 at 2:57 pm

  44. i show one full post at a time on my front page – so i’m wondering if anyone has adapted this to work with posts instead of pages.

    thanks again for the great plug in. cheers.

    Posted by g.lo on January 14th, 2010 at 12:48 pm

  45. Posted by Stephanie on January 14th, 2010 at 3:28 pm

  46. I think you need to add to a post to break it down if too long, or else you will have a really long post. That is what I use on my pages and posts when they get too long.

    Posted by Ed on January 15th, 2010 at 12:28 pm

  47. looks like it ate up the `` in my previous comment.

    Posted by Ed on January 15th, 2010 at 12:29 pm

  48. Not sure but I use “” to break them up.

    Posted by Ed on January 15th, 2010 at 12:30 pm

  49. For some reason my “nextpage” between tags is getting chewed up in the comments.

    Posted by Ed on January 15th, 2010 at 12:31 pm

  50. Ed, as the comment form says, Textile formatting is in effect on this site. Alternatively, just escape your angle brackets.

    Posted by Stephanie on January 15th, 2010 at 4:03 pm

  51. Ahh! Thank you for pointing that out.

    Posted by Ed on January 18th, 2010 at 12:19 pm

  52. Thanks for a great piece of code. (I’m using the code directly in a page file as opposed to the plugin.)

    The code works perfectly except for the fact it somehow prohibits @@ from rendering. Instead, it displays the page ID. Not sure why this happens. Have you encountered this before?

    Posted by Tracy on January 18th, 2010 at 12:36 pm

  53. Tracy, your code didn’t quite come through. Try again, please?

    Posted by Stephanie on January 18th, 2010 at 12:46 pm

  54. Oh, sorry. I’ll just state it. It didn’t render “the_content”. It did render everything else on the page, though. And I was wrong about it displaying the page ID. It displays “371″ on each page where “the_content” is supposed to be.

    Posted by Tracy on January 18th, 2010 at 12:55 pm

  55. How bizarre. Do you have a link I can look at? You can send it privately (stephanieleary at Google’s email service) if you prefer.

    Posted by Stephanie on January 18th, 2010 at 1:47 pm

  56. Posted by Stephanie on January 18th, 2010 at 1:49 pm

  57. Unfortunately I’m in test mode on the site, so unless I make you an admin, you won’t be able to see it.

    I tried the debugging code you sent me and it appears that the first page in the array has an ID of 371. So somehow it’s printing that in the content area.

    Here’s the printed debug: Array ( [0] => 371 [1] => 368 [2] => 376 [3] => 369 [4] => 370 [5] => 374 [6] => 372 [7] => 373 [8] => 377 [9] => 366 ) Current page ID’s array key: 4Previous page ID: 369Next page ID: 374

    It’s so weird! I have no idea why it would print that in the content. Oh, and I tried moving the entire code after the content, and the content appears fine. So somehow when it appears before the content it’s telling the content to print 371.

    Posted by Tracy on January 18th, 2010 at 4:55 pm

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

    Posted by Ed on January 19th, 2010 at 2:41 pm

  59. 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.

    Posted by Tracy on January 19th, 2010 at 4:38 pm

  60. 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.

    Posted by Stephanie on January 19th, 2010 at 4:59 pm

  61. 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!

    Posted by Tracy on January 19th, 2010 at 5:32 pm

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

    Posted by Ed on January 20th, 2010 at 11:52 am

  63. @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.)

    Posted by Tracy on January 20th, 2010 at 1:21 pm

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

    Posted by Stephanie on January 20th, 2010 at 1:24 pm

  65. Thanks @Stephanie and @Tracy for all the help.

    Posted by Ed on January 20th, 2010 at 1:50 pm

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

    Posted by Tracy on January 20th, 2010 at 5:11 pm

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

    Posted by Ed on January 21st, 2010 at 2:03 pm

  68. 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!

    Posted by Simon on January 30th, 2010 at 3:25 am

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

    Posted by Ed on February 1st, 2010 at 2:05 pm

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

    Posted by Ed on February 6th, 2010 at 6:28 pm

Leave a Reply

-- or --

Textile formatting is in effect.

RSS feed for comments on this post. TrackBack URI