Title: Page depth not working on function
Last modified: August 19, 2016

---

# Page depth not working on function

 *  [lukekfreeman](https://wordpress.org/support/users/lukekfreeman/)
 * (@lukekfreeman)
 * [15 years, 12 months ago](https://wordpress.org/support/topic/page-depth-not-working-on-function/)
 * Hi all,
 * I’m trying to use get_pages to list pages in a more sophisticated manner than
   wp_list_pages, however to get the ULs to work properly with the depths then I
   need to find each page’s depth, however, I created a function to get the page
   depth but it doesn’t work on the subpages.
 * This is the function:
 *     ```
       function pagedepth($page)
       {
       	$ancestors = get_post_ancestors($page->ID);
       	return count($ancestors);
       }
       ```
   
 * And this is the list pages:
 *     ```
       function listpoint($pg)
       		{
       			echo pagedepth($pg);
       			echo '<li><a href="';
       			echo get_page_link($pg->ID);
       			echo '">';
       			echo $pg->post_title;
       			echo '</a> ';
       			echo get_userdata($pg->post_author)->display_name;
       			echo '—';
       				if (get_post_custom_values('Character', $pg->ID)==TRUE)
       				{
       				  $mykey_values = 	get_post_custom_values('Character', $pg->ID);
       				  foreach ( $mykey_values as $key => $value )
       					{
       				    echo $value;
       					echo ', ';
       				 	}
       				}
       			echo mysql2date('D, M j, Y H:i a', $pg->post_date);
       			echo '</li>';
       		}
   
       		function listpages($thepost){
       			$pages = get_pages('child_of='.$thepost->ID.'&sort_column=post_date&sort_order=DESC');
       			$previousdepth = 0;
       			$homedepth = pagedepth($thepost);
       			foreach($pages as $page)
       			{
       				$pagedepth = pagedepth($page);
       				if($pagedepth > $previousdepth)
       					echo '<ul>';
       				while($pagedepth < $previousdepth && count(get_pages('child_of='.$page->ID)) != 0)
       					{ echo '</ul>';
       					$pagedepth--;
       					if($pagedepth = 1)
       					break;
       					}
       				$parent_page = $page->post_parent;
       				$content = $page->post_content;
       				if(!$content)
       					continue;
       				$content = apply_filters('the_content', $content);
       				listpoint($page);
       				$previousdepth = $pagedepth;
       			}
       	}
       ```
   
 * Any help would be greatly appreciated.
 * Thanks.
 * Cheers,
 * Luke

The topic ‘Page depth not working on function’ is closed to new replies.

 * 0 replies
 * 1 participant
 * Last reply from: [lukekfreeman](https://wordpress.org/support/users/lukekfreeman/)
 * Last activity: [15 years, 12 months ago](https://wordpress.org/support/topic/page-depth-not-working-on-function/)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
