On my Wordpress webpage, I have several Pages. Now I want to display "Next Page" & "Previous Page" links. I know that there are several ways out there to do this, but they all fail because I want to skip one page using this link system.

The existing Wordpress functions for this can exclude pages, but if the next page is the page that should be excluded, no link is displayed. I need a solution to just skip the excluded page.

As my English isn't best, here is an example of what I need:
The current page id is 3.
The page I want to exclude has page id 4.
So the "Next page" link should point to page id 5, the "Previous page" link to page id 2.

How is this possible?

I Actually tried this by myself and was able to get the ID of the next page using get_pages and some PHP functions, but I failed to exclude the page. If needed I can post the code for that.

Please help me!

1 Answer 1


I just tried this and it works for me.

Given that you would like to exclude page id 4 this is the code you are looking for.

        $pagelist = get_pages('sort_column=menu_order&sort_order=asc&exclude=4');
        $pages = array();
        foreach ($pagelist as $page) {
           $pages[] += $page->ID;

        $current = array_search($post->ID, $pages);
        $prevID = $pages[$current-1];
        $nextID = $pages[$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>
        <?php }
        if (!empty($nextID)) { ?>
        <div class="alignright">
        <a href="<?php echo get_permalink($nextID); ?>" 
         title="<?php echo get_the_title($nextID); ?>">Next</a>
        <?php } ?>
  • Thanks for your reply! That script doesn't display a "previous" link for me and it jumps straight to the last page when clicked on "next". I believe the weak point of that script is that part: $prevID = $pages[$current-1]; $nextID = $pages[$current+1]; because the IDs aren't always consecutive.
    – Sven
    Commented Jan 8, 2013 at 14:30
  • well, that's unfortunate. but in my case i've few pages which ids are 2,6,8,10 and 12 so non-consecutive ids for me too. I used that code to exclude id 8 and the next/prev links are to /?page_id=6 and /?page_id=10
    – molokom
    Commented Jan 8, 2013 at 14:40
  • Thank you! I fixed the problem, $post->ID wasn't working. Now everything is fine!
    – Sven
    Commented Jan 8, 2013 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.