next/previous page from an array of page id’s

I have an array of page ID’s which I need to use to create a simple next/previous page menu.

Below you can see what I have put together so far.

Read More

I realize I am not even close here…

    <?php
        $pagelist = array(0,1358,226,1394,1402,1468,0);

        $prevID = prev($pagelist);
        $nextID = next($pagelist);
    ?>

    <div class="navigation">
        <?php if ($prevID != 0) { ?>
        <div class="alignleft">
            <a href="<?php echo get_permalink($prevID); ?>" title="<?php echo get_the_title($prevID); ?>">Previous</a>
        </div>
        <?php } ?>
        <?php if ($nextID != 0) { ?>
        <div class="alignright">
            <a href="<?php echo get_permalink($nextID); ?>" title="<?php echo get_the_title($nextID); ?>">Next</a>
        </div>
        <?php } ?>
    </div><!-- .navigation -->

I think I need to use the current page ID at some point which I get using this function

<?php the_ID(); ?>

Could anybody help point me in the right direction please?

Related posts

Leave a Reply

1 comment

  1. assuming that your URL contain id parameter :

    <?php
            $pagelist = array(0,1358,226,1394,1402,1468,0);
    
            $currentIndex = array_search($_GET['id'], $pagelist); // $_GET['id'] may be replace by your the_ID() function
    
            $prevID = $currentIndex - 1 < 0 ? $pagelist[0] : $pagelist[$currentIndex - 1];
            $nextID = $currentIndex + 1 > count($pagelist)-1 ? $pagelist[count($pagelist)-1] : $pagelist[$currentIndex + 1];
        ?>
    
        <div class="navigation">
            <?php if ($prevID != 0) { ?>
            <div class="alignleft">
                <a href="<?php echo get_permalink($prevID); ?>" title="<?php echo get_the_title($prevID); ?>">Previous</a>
            </div>
            <?php } ?>
            <?php if ($nextID != 0) { ?>
            <div class="alignright">
                <a href="<?php echo get_permalink($nextID); ?>" title="<?php echo get_the_title($nextID); ?>">Next</a>
            </div>
            <?php } ?>
        </div><!-- .navigation -->
    

    Hope this solution may help you