Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- This code will (I hope) use wp_list_pages to list all but the last sublevel in a
- hierarchy of pages. It is in response to this WP Support topic:
- http://wordpress.org/support/topic/dynamically-limit-depth-of-wp_list_pages
- */
- $parent = 2192; // The ID of the parent Page
- function array_max_depth($array, $depth = 0) {
- // The array here is and entry in the $refs array.
- // The max depth is the sum of:
- // 1 for the parent
- // 1 for each 'children' element
- // 1 for each child ID in 'children'
- // To get the true depth, subtract 1 for the parent, divide by 2 to get
- // the number of children, and add back the 1 for the parent.
- // true depth = ((max_depth - 1) / 2) + 1
- $max_sub_depth = 0;
- foreach (array_filter($array, 'is_array') as $subarray) {
- $max_sub_depth = max(
- $max_sub_depth,
- array_max_depth($subarray, $depth)
- );
- }
- return $max_sub_depth + $depth + 1;
- }
- function get_includes($keys,&$includes,&$parent_data) {
- // Recursively walk $parent_data accumulating included page ids
- foreach ($keys as $parentid) {
- // print_r('<p><pre>KEYS: ');print_r($keys);print_r('<br />');
- $this_data = $parent_data[$parentid];
- // print_r("THISDATA: $parentid ");print_r($this_data);print_r('<br />');
- if ($this_data['maxdepth'] > 1) {
- $includes = array_merge($includes,array_keys($this_data['children']));
- // print_r("<br />INCLUDES: ");print_r($includes);print_r('</pre></p>');
- get_includes(array_keys($this_data['children']), $includes, $parent_data);
- }
- }
- }
- $pages = get_pages(array('child_of' => $parent));
- //print_r($pages);
- if($pages) {
- $refs = array();
- foreach ($pages as $page) {
- // Create $refs as an array of page IDs with keys 'post_parent',
- // 'post_title', and 'children'. The 'children' key contains an array of
- // IDs of children of this page.
- $data = array('ID' => $page->ID, 'post_parent' => $page->post_parent, 'post_title' => $page->post_title );
- $thisref = &$refs[ $data['ID'] ];
- $thisref['post_parent'] = $data['post_parent'];
- $thisref['post_title'] = $data['post_title'];
- $refs[ $data['post_parent'] ]['children'][ $data['ID'] ] = &$thisref;
- }
- //print_r('<p><pre>REFS: ');print_r($refs);print_r('</pre></p>');
- $new_parent = array('children' => array(), 'maxdepth' => 0);
- $parents[$parent] = $new_parent;
- foreach ($pages as $page) {
- if (false === array_search($page->post_parent,array_keys($parents),true)) $parents[$page->post_parent] = $new_parent;
- $indent = array_search($page->post_parent,array_keys($parents),true);
- $depth = ((array_max_depth($refs[$page->ID]) - 1) / 2 ) + 1;
- $parents[$page->post_parent]['children'][$page->ID] = $depth;
- if ($depth > $parents[$page->post_parent]['maxdepth']) $parents[$page->post_parent]['maxdepth'] = $depth;
- // echo str_repeat(' ',$indent * 5);
- // echo "ID: $page->ID PARENT:$page->post_parent DEPTH:$depth $TITLE:$page->post_title<br />";
- }
- // print_r('<p><pre>PARENTS: ');print_r($parents);print_r('</pre></p>');
- $includes = array_merge(array($parent),array_keys($parents[$parent]['children'])); // Always include the parent and its direct children
- // print_r('<p><pre>INCLUDES: ');print_r($includes);print_r('</pre></p>');
- get_includes(array_keys($parents[$parent]['children']),$includes,$parents);
- // print_r('<p><pre>INCLUDES: ');print_r($includes);print_r('</pre></p>');
- // Now use $includes in the args to wp_list_pages.
- $args=array(
- 'title_li' => 'Tree of Parent Page ' . $parent,
- 'include' => implode(",", $includes),
- 'echo' => 0,
- );
- $page_nav = wp_list_pages( $args );
- // print_r(htmlentities($page_nav));print_r('<br />');
- echo $page_nav;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement