Get next/previous cousin page

时间:2011-05-10 作者:Drew Baker

嘿,伙计们,我正在尝试为下一页/上一页建立分页链接。例如,我的页面结构如下:

当我在a2上时,我希望“上一页”链接转到a1,但“下一页”链接转到b1(跳过B)。

我在用greatNext Page, Not Next Post 插件,但它只能转到下一页(因此在上面的示例中会包含B)。

我试图修改它,只显示堂兄弟,但遇到了一些麻烦。到目前为止,我有:

    // 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.\'">\';
    }
那么我该如何跳过B,转到b1等等?

谢谢你的帮助!

-德鲁

1 个回复
最合适的回答,由SO网友:Jan Fabry 整理而成

我通过获取所有页面,按级别分配它们,然后获取同一级别的下一页来解决这个问题。如果还需要上一页,则可能应该缓存按级别对页面分组的功能。

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;
}

结束

相关推荐

过滤NEXT_POSTS_LINK和PREVICE_POSTS_LINK的URL

我正在与qTranslate plugin 创建多语言网站。除了在next_posts_link &;previous_posts_link 模板标记。当用户选择了其他语言时,URL应更改为:http://mysite.com/test/page/2 (对于默认语言)到http://mysite.com/fr/test/page/2 (法语)该插件提供了使用qtrans_convertURL($url) 作用我遇到的问题是找到一个合适的钩子或过滤器,允许我通过next\\u posts\\u lin