Sillybean

Next Page

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

Download it at wordpress.org »

Report problems using the support forum »

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&#038;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');
141 comments

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

141 Responses to “Next Page”

  1. TheBeeOBee says:

    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!

  2. Stephanie says:

    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!

  3. TheBeeOBee says:

    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.

  4. Aion Vor says:

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

  5. Stephanie says:

    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?

  6. Stephanie says:

    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.

  7. Jeff K says:

    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.

    • Stephanie says:

      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.

  8. Logan says:

    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.

  9. Belinda says:

    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

  10. Belinda says:

    Yes, that would be fabulous!

  11. Stephanie says:

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

  12. Belinda says:

    WOW. Thanks so much!!

  13. Logan says:

    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!

    • Stephanie says:

      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!

  14. Logan says:

    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!

  15. big will says:

    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!

  16. Ed says:

    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.

  17. Wee says:

    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!

  18. Wee says:

    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.

  19. Stinky says:

    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?

  20. Stephanie says:

    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.

  21. Stinky says:

    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!

    • Stephanie says:

      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!

  22. Stinky says:

    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?

  23. Stinky says:

    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?

  24. Stinky says:

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

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

  25. Ed says:

    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.

  26. Stephanie says:

    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. :(

  27. Stinky says:

    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.

  28. Ed says:

    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.

  29. John Cardell says:

    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.

  30. Ed says:

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

  31. Derek says:

    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.

  32. Ed says:

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

  33. Will says:

    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!

  34. Ed says:

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

  35. Will says:

    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.

  36. Ed says:

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

  37. George says:

    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.

  38. Ed says:

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

  39. g.lo says:

    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.

  40. Ed says:

    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.

  41. Ed says:

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

  42. Ed says:

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

  43. Ed says:

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

  44. Stephanie says:

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

  45. Ed says:

    Ahh! Thank you for pointing that out.

  46. Tracy says:

    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?

  47. Tracy says:

    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.

  48. Stephanie says:

    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.

  49. Tracy says:

    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.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>