Advertisement
vtxyzzy

List only upper level Pages

Jan 19th, 2013
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.84 KB | None | 0 0
  1. <?php
  2. /*
  3. This code will (I hope) use wp_list_pages to list all but the last sublevel in a
  4. hierarchy of pages.  It is in response to this WP Support topic:
  5.  
  6. http://wordpress.org/support/topic/dynamically-limit-depth-of-wp_list_pages
  7. */
  8.  
  9. $parent = 2192; // The ID of the parent Page
  10.  
  11. function array_max_depth($array, $depth = 0) {
  12.    // The array here is and entry in the $refs array.
  13.    // The max depth is the sum of:
  14.    //    1 for the parent
  15.    //    1 for each 'children' element
  16.    //    1 for each child ID in 'children'
  17.    // To get the true depth, subtract 1 for the parent, divide by 2 to get
  18.    // the number of children, and add back the 1 for the parent.
  19.    // true depth = ((max_depth - 1) / 2) + 1
  20.    $max_sub_depth = 0;
  21.    foreach (array_filter($array, 'is_array') as $subarray) {
  22.       $max_sub_depth = max(
  23.          $max_sub_depth,
  24.          array_max_depth($subarray, $depth)
  25.       );
  26.     }
  27.     return $max_sub_depth + $depth + 1;
  28. }
  29.  
  30. function get_includes($keys,&$includes,&$parent_data) {
  31.    // Recursively walk $parent_data accumulating included page ids
  32.    foreach ($keys as $parentid) {
  33.       // print_r('<p><pre>KEYS: ');print_r($keys);print_r('<br />');
  34.       $this_data = $parent_data[$parentid];
  35.       // print_r("THISDATA: $parentid ");print_r($this_data);print_r('<br />');
  36.  
  37.       if ($this_data['maxdepth'] > 1) {
  38.          $includes = array_merge($includes,array_keys($this_data['children']));
  39.          // print_r("<br />INCLUDES: ");print_r($includes);print_r('</pre></p>');
  40.  
  41.          get_includes(array_keys($this_data['children']), $includes, $parent_data);
  42.       }
  43.    }
  44. }
  45.  
  46. $pages = get_pages(array('child_of' => $parent));
  47. //print_r($pages);
  48.  
  49. if($pages) {
  50.    $refs = array();
  51.  
  52.    foreach ($pages as $page) {
  53.       // Create $refs as an array of page IDs with keys 'post_parent',
  54.       // 'post_title', and 'children'.  The 'children' key contains an array of
  55.       // IDs of children of this page.
  56.  
  57.       $data = array('ID' => $page->ID, 'post_parent' => $page->post_parent, 'post_title' => $page->post_title );
  58.       $thisref = &$refs[ $data['ID'] ];
  59.  
  60.       $thisref['post_parent'] = $data['post_parent'];
  61.       $thisref['post_title'] = $data['post_title'];
  62.  
  63.       $refs[ $data['post_parent'] ]['children'][ $data['ID'] ] = &$thisref;
  64.    }
  65.    //print_r('<p><pre>REFS: ');print_r($refs);print_r('</pre></p>');
  66.  
  67.    $new_parent = array('children' => array(), 'maxdepth' => 0);
  68.    $parents[$parent] = $new_parent;
  69.    foreach ($pages as $page) {
  70.       if (false === array_search($page->post_parent,array_keys($parents),true)) $parents[$page->post_parent] = $new_parent;
  71.       $indent = array_search($page->post_parent,array_keys($parents),true);
  72.       $depth =  ((array_max_depth($refs[$page->ID]) - 1) / 2 ) + 1;
  73.       $parents[$page->post_parent]['children'][$page->ID] = $depth;
  74.       if ($depth > $parents[$page->post_parent]['maxdepth']) $parents[$page->post_parent]['maxdepth'] = $depth;
  75.       // echo str_repeat('&nbsp;',$indent * 5);
  76.       // echo "ID: $page->ID  PARENT:$page->post_parent DEPTH:$depth $TITLE:$page->post_title<br />";
  77.    }
  78.    // print_r('<p><pre>PARENTS: ');print_r($parents);print_r('</pre></p>');
  79.  
  80.    $includes = array_merge(array($parent),array_keys($parents[$parent]['children']));  // Always include the parent and its direct children
  81.    // print_r('<p><pre>INCLUDES: ');print_r($includes);print_r('</pre></p>');
  82.  
  83.    get_includes(array_keys($parents[$parent]['children']),$includes,$parents);
  84.    // print_r('<p><pre>INCLUDES: ');print_r($includes);print_r('</pre></p>');
  85.  
  86.   // Now use $includes in the args to wp_list_pages.
  87.   $args=array(
  88.     'title_li' => 'Tree of Parent Page ' . $parent,
  89.     'include' => implode(",", $includes),
  90.     'echo' => 0,
  91.   );
  92.   $page_nav = wp_list_pages( $args );
  93.   // print_r(htmlentities($page_nav));print_r('<br />');
  94.   echo $page_nav;
  95. }
  96.  
  97. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement