Get next/previous cousin page

Hey Guys,
I’m trying to build pagination links for the next/previous cousin page. For example, my page structure is like this:

  • Top Level
    • A
      • a1
      • a2
    • B
      • b1
      • b2

When I’m on a2, I want the ‘Previous Page’ link to go to a1, but the ‘Next Page’ link to go to b1 (skipping B).

Read More

I’m using great Next Page, Not Next Post plugin at the moment, but it can only go to next page (so it would include B in the above example).

I’m trying to modify it to only show cousins, but running into some trouble. So far I have this:

    // query gand parent page level, go to next parent page, and then loop to find it's next child, eke!
    // get grandparent id
    $parentInfo = get_page($post->post_parent);

    // query the level above's pages
    // $getParentPages = get_pages('child_of='.$parentInfo->post_parent.'&parent='.$parentInfo->post_parent.'&'.$getPagesQuery);
    $getParentPages = get_pages($getPagesQuery);
    $parentPageCount = count($getParentPages);      

    for($pp=0; $pp < $parentPageCount; $pp++) {
        // get the array key for our entry
        // if ($post->post_parent == $getParentPages[$pp]->ID) break;
        if ($post->ID == $getParentPages[$pp]->ID) break;
    }

    // assign our next key
    $parentNextKey = $pp+1;

    if (isset($getParentPages[$parentNextKey])) {
        $anchorName = $getParentPages[$parentNextKey]->post_title;

        $output = '<a href="'.get_permalink($getParentPages[$parentNextKey]->ID).'" title="'.$anchorName.'">';
    }

So how would I make that skip B, and go to b1 and so on?

Thanks for the help!

-Drew

Related posts

Leave a Reply

1 comment

  1. I solved this by getting all pages, assigning them by level, and then getting the next page of the same level. If you also want the previous page then you should probably cache the functionality that groups the pages by level.

    function wpse16875_next_page_same_level_link()
    {
        $next_page_same_level = wpse16875_next_page_same_level();
        if ( $next_page_same_level ) {
            return '<a href="' . get_permalink( $next_page_same_level ) . '">' . get_the_title( $next_page_same_level ) . '</a>';
        }
        return '';
    }
    
    function wpse16875_next_page_same_level()
    {
        $current_page = $GLOBALS['post'];
        $current_page_level = null;
    
        $all_pages = get_pages();
        $pages_by_id = array();
        $page_ids_by_parent_id = array();
        $page_ids_by_level = array();
        foreach ( $all_pages as $page ) {
            $pages_by_id[$page->ID] = $page;
            if ( ! array_key_exists( $page->post_parent, $page_ids_by_parent_id ) ) {
                $page_ids_by_parent_id[$page->post_parent] = array();
            }
            $page_ids_by_parent_id[$page->post_parent][] = $page->ID;
        }
        foreach ( $all_pages as $page ) {
            $ancestor_page_id = $page->post_parent;
            $level = 0;
            while ( 0 != $ancestor_page_id ) {
                $ancestor_page_id = $pages_by_id[$ancestor_page_id]->post_parent;
                $level++;
            }
            if ( $page->ID == $current_page->ID ) {
                $current_page_level = $level;
            }
            if ( ! array_key_exists( $level, $page_ids_by_level ) ) {
                $page_ids_by_level[$level] = array();
            }
            $page_ids_by_level[$level][] = $page->ID;
        }
    
        $current_level_page_ids = $page_ids_by_level[$current_page_level];
    
        $current_page_pos_by_level = array_search( $current_page->ID, $current_level_page_ids );
        if ( array_key_exists( $current_page_pos_by_level + 1, $current_level_page_ids ) ) {
            return $pages_by_id[$current_level_page_ids[$current_page_pos_by_level + 1]];
        }
        return null;
    }