WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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!

share|improve this question
up vote 1 down vote accepted

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 } ?>
share|improve this answer
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 Jan 8 '13 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 Jan 8 '13 at 14:40
Thank you! I fixed the problem, $post->ID wasn't working. Now everything is fine! – Sven Jan 8 '13 at 14:58

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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