Christoph
Christoph

Reputation: 543

Wordpress wp_list_pages: list parent page siblings

I'm trying to get this working for quite a while now. I get close but can't find a working solution.

My page structure is like this:

1 FIRST LEVEL
  2 SECOND LEVEL
    3 THIRD LEVEL
      4 CAR
         5 SUV
         5 MINIVAN
         5 FULLSIZE
      4 MOTORCYCLE
      4 BICYCLE
  2 SECOND LEVEL
  2 SECOND LEVEL
1 FIRST LEVEL

I need a wp_list_pages statement that does the following: on 4th and 5th level pages (and ONLY on those) the navigation lists all 4th level pages.

(When i'm on page "CAR" the navigation should be CAR,MOTORCYCLE,BICYCLE. And also when i'm on page MINIVAN the navigation should be CAR,MOTORCYCLE,BICYCLE.)

Thank you very much for any help!

Upvotes: 1

Views: 3551

Answers (2)

Christoph
Christoph

Reputation: 543

Here's the code i finally used, thanks to TheDeadMedic's answer. (I canceled one page level so it's for 3rd and 4th level)

$ancestors = get_post_ancestors($post);
if (count($ancestors) == 2) {
   $parent = $ancestors[0];
}
elseif (count($ancestors) == 3) {
   $parent = $ancestors[1];
}

if ($parent) { 
   $tabs = wp_list_pages('depth=1&child_of=' . $parent);
}

Upvotes: 1

TheDeadMedic
TheDeadMedic

Reputation: 9997

$ancestors = get_post_ancestors($post);
if (count($ancestors) >= 3) {
    $parent = $ancestors[2]; // third level page ID
    wp_list_pages('depth=1&child_of=' . $parent);
}

Feel free to ask if you're unsure of anything :)

Upvotes: 4

Related Questions