• Resolved robbiegod

    (@robbiegod)


    Page: http://codex.wordpress.org/Next_and_Previous_Links

    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];
    $nextID = $pages[$current+1];
    ?>
    
    <div class="navigation">
    <?php if (!empty($prevID)) { ?>
    <div class="alignleft">
    <a>"
      title="<?php echo get_the_title($prevID); ?>">Previous</a>
    </div>
    <?php }
    if (!empty($nextID)) { ?>
    <div class="alignright">
    <a>"
     title="<?php echo get_the_title($nextID); ?>">Next</a>
    </div>
    <?php } ?>
    </div><!-- .navigation -->

    The error I see is when i am on either first or last posts of my custom post type. There is no previous post on the first post and then there is no next post on the last post.

    “Notice: Undefined offset: -1 in…on line 47”.

    Questions:

    1. How i get the notice to stop appearing?
    2. is there a way to make my first post have the last post as its previous link?
    3. is there a way to make my last post have the first post as its next link?

    #2 and #3 combined would create an infinite loop for my posts. So that once they hit the end they can start back at the beginning.

Viewing 1 replies (of 1 total)
  • Thread Starter robbiegod

    (@robbiegod)

    This should fix it. Maybe the codex should be updated to avoid the notice? Not saying this solution is better or anything, maybe someone has a better option. This will work for me however.

    $pagelist = get_pages('post_type=page&child_of=104');
    $pages = array();
    foreach ($pagelist as $page) {
       $pages[] += $page->ID;
    }
    
    $current = array_search(get_the_ID(), $pages);
    
    if(!empty($pages[$current-1])):
    	$prevID = $pages[$current-1];
    else:
    	$prevID = "1009";
    endif;
    
    if(!empty($pages[$current+1])):
    	$nextID = $pages[$current+1];
    else:
    	$nextID = "998";
    endif;
Viewing 1 replies (of 1 total)

The topic ‘Next/Previous Links in Codex sample code displays an error’ is closed to new replies.