wordpress query menu all children of top level par

2019-08-17 08:30发布

问题:

I'm lost :) I tried many solutions byt I failed. I need to write the code to create menu of all children of current main (top level) parent. Pages structure looks like this:

  • Homepage
  • Page A
    • Subpage A1
    • Subpage A2
      • Sub-subpage A2-1
      • Sub-subpage A2-2
  • Page B
    • Subpage B1
    • Subpage B2
      • Sub-subpage B2-1
      • Sub-subpage B2-2
    • Subpage B3
  • Subpage C

So, if I'm on Page A or Subpage A2 or Subpage A2-1 ... i want to list the same elements:

  • Subpage A1
  • Subpage A2
    • Sub-subpage A2-1
    • Sub-subpage A2-2

All children of current top level parent (Page A), doesn't metter how 'deep' we're. Any help? :)

回答1:

Here is the answer:

<?php
    if ($post->post_parent) {
        $ancestors=get_post_ancestors($post->ID);
        $root=count($ancestors)-1;
        $parent = $ancestors[$root];
    } else {
        $parent = $post->ID;
    }
    echo '<ul>';
    wp_list_pages('orderby=name&depth=4&order=DESC&show_count=0&child_of='.$parent.'&title_li=');
    echo '</ul>';
?>