Forums

List parents siblings with wp_list_pages (page) (7 posts)

  1. HarleyB
    Member
    Posted 10 months ago #

    Hi there,

    I'm trying to list the parents siblings of a page.

    I've just spent way too long trying to find a way to do this.
    I thought I'd find the pages parents parent ID, then do something like this:

    `wp_list_pages('title_li=&child_of='.$secondParentsID);

    However I continue to fail...
    Any help would be greatly appreciated.

    Thank you!

  2. alchymyth
    The Sweeper
    Posted 10 months ago #

    this might work:

    <?php if( is_page() && $post->post_parent ) :
    wp_list_pages('title_li=&parent='.$post->post_parent);
    endif; ?>

    the conditional helps to avoid to show the page list in the wrong situations;
    and the 'parent' parameter will show only the direct child level
    (not documented in the codex chapter for 'wp_list_pages()' , but seems to work analogous to 'get_pages()' )

    alternatively, try:

    <?php if( is_page() && $post->post_parent ) :
    wp_list_pages('title_li=&child_of='.$post->post_parent.'&depth=1');
    endif; ?>

    http://codex.wordpress.org/Function_Reference/wp_list_pages

  3. HarleyB
    Member
    Posted 10 months ago #

    Unfortunately, that only lists the current pages siblings - I'm looking to list the parents siblings.
    Thanks nonetheless..!

  4. IntlOrange
    Member
    Posted 10 months ago #

    I want to do the same thing. It seems there's no built-in WP function for listing a specific page's siblings — only children.

  5. alchymyth
    The Sweeper
    Posted 10 months ago #

    just have to go up one level:

    <?php if( is_page() && get_post($post->post_parent)->post_parent ) :
    wp_list_pages('title_li=&parent='.get_post($post->post_parent)->post_parent);
    endif; ?>
  6. IntlOrange
    Member
    Posted 10 months ago #

    Thanks, alchymyth, for showing us how to traverse up the tree of posts/pages. Your solution is more elegant than another one I just found:

    http://stackoverflow.com/questions/4314918/wordpress-wp-list-pages-list-parent-page-siblings/4316179#4316179

  7. Bermuda Sam
    Member
    Posted 5 months ago #

    Is there a way to include both the parent page's siblings and the current page's siblings at the same time? Maybe even going one step deeper and showing current page's children..

    This is to create a tree-like navigation, like this:

    |- Parent sibling #1
    |- Parent
    |-- Current page sibling #1
    |-- Current page
    |--- Child of current page #1
    |--- Child of current page #2
    |-- Current page sibling #2
    |- Parent sibling #2

Reply

You must log in to post.

About this Topic