• Buiding a new theme on local host I added page navigation code to the page.php. It works like charm but error messages appear on the first and the last page. I wonder how these errors can be solved? I did use the code given on: https://codex.www.ads-software.com/Next_and_Previous_Links, but I added a Home link in between Previous and Next.
    Here is my code; I also added some comments in the code to point out the problem:

    <!--Navigation code-->
    		<?php
    		$pagelist = get_pages('sort_column=menu_order&sort_order=asc');
    		$pages = array();
    		foreach ($pagelist as $page) {
    		   $pages[] += $page->ID;
    		}
    
    		$current = array_search(get_the_ID(), $pages);
    		$prevID = $pages[$current-1]; /*error message: Notice: Undefined offset: -1 in first page*/
    		$nextID = $pages[$current+1]; /*error message: Notice: Undefined offset: 18 in last page*/
    		?>
    
    		<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 Page &laquo</a>
    		</div>
    		<a href="<?php echo home_url(); ?>">Home</a>
    		<?php }	if (!empty($nextID)) { ?>
    		<div class="alignright">
    		<a href="<?php echo get_permalink($nextID); ?>"
    		 title="<?php echo get_the_title($nextID); ?>">&raquo Next Page</a>
    		</div>
    		<?php } ?>
    		</div><!--Navigation-->
    	<!--Navigation code end-->

    [ No bumping please. ]

  • The topic ‘Page navigation code error on first and last page’ is closed to new replies.