Support » Developing with WordPress » Getting next/prev page ID from array

  • Resolved peterjem

    (@peterjem)


    I have a wordpress parent page with child and its grand child pages in following structure.

    -Parent page

      childpage 1
    • Grandchild 1 (page id = 5)
    • Grandchild 2 (page id = 9)
    • Grandchild 3 (page id = 11)
      Childpage 2
    • Grandchild 4 (page id = 15)
    • Grandchild 5 (page id = 22)
    • I am trying to do a custom “The Next and Previous Pages” function with only grandchild pages as pages. so i have an array with page ID’s as follows

      $pages = array(‘1’=>’5’, ‘2’=>’9′, ‘3’=>’11’, ‘4’=>’15’, ‘5’=>’22’);

      I tried to make use of steps mentioned in https://codex.wordpress.org/Next_and_Previous_Links#The_Next_and_Previous_Pages, but ‘Next Page’ link is not appearing in Grandchild 3′ and ‘Previous page’ link in ‘Grandchild 4’ page, is there is any way to make this work?

      Thanks

      My code

      <?php
      $pages = array('1'=>'5', '2'=>'9', '3'=>'11', '4'=>'15', '5'=>'22');    
      $current = array_search(get_the_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>
      </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 -->
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.